00001
00009 #include <stdlib.h>
00010 #include "pqueue.h"
00011
00012
00013 PQueue newPQueue(int(*comp)(void*,void*))
00014 {
00015 PQueue pqueue=(PQueue)malloc(sizeof(SPQueue));
00016
00017 if(!pqueue) return NULL;
00018 else
00019 {
00020 pqueue->comp=*comp;
00021 pqueue->size=0;
00022 pqueue->head=NULL;
00023 return pqueue;
00024 }
00025 }
00026
00027
00028
00029 int pqueueSetComp(PQueue pqueue,int(*comp)(void*,void*))
00030 {
00031 if(!comp) return 1;
00032 else
00033 {
00034 pqueue->comp=*comp;
00035 return 0;
00036 }
00037 }
00038
00039
00040
00041 void pqueueDelete(PQueue pqueue)
00042 {
00043 PQueueNode aux1,aux2;
00044
00045 if(!pqueue->size)
00046 free(pqueue);
00047 else
00048 {
00049 for(aux1=pqueue->head;aux1;)
00050 {
00051 aux2=aux1;
00052 aux1=aux1->next;
00053 free(aux2);
00054 }
00055 free(pqueue);
00056 }
00057 }
00058
00059
00060
00061 int pqueueInsert(PQueue pqueue,void *inf)
00062 {
00063 PQueueNode new,this,prev;
00064 new=(PQueueNode)malloc(sizeof(SPQueueNode));
00065
00066 if(new)
00067 {
00068 new->inf=inf;
00069
00070 if(!pqueue->size)
00071 {
00072 new->next=NULL;
00073 pqueue->head=new;
00074 pqueue->size++;
00075 }
00076 else
00077 {
00078 for(this=pqueue->head,prev=NULL;
00079 this&&((pqueue->comp)(this->inf,inf)<=0);
00080 prev=this,this=this->next);
00081
00082 if(prev)
00083 {
00084 new->next=prev->next;
00085 prev->next=new;
00086 }
00087 else
00088 {
00089 new->next=pqueue->head;
00090 pqueue->head=new;
00091 }
00092 pqueue->size++;
00093 }
00094
00095 return 0;
00096 }
00097 else return 1;
00098 }
00099
00100
00101
00102 int pqueueRemove(PQueue pqueue,void **inf)
00103 {
00104 PQueueNode aux;
00105
00106 if(!pqueue->size)
00107 {
00108 if(inf) *inf=NULL;
00109 return 1;
00110 }
00111 else
00112 {
00113 if(inf) *inf=pqueue->head->inf;
00114
00115 if(pqueue->size==1)
00116 {
00117 free(pqueue->head);
00118 pqueue->head=NULL;
00119 }
00120 else
00121 {
00122 aux=pqueue->head;
00123 pqueue->head=pqueue->head->next;
00124 free(aux);
00125 }
00126
00127 pqueue->size--;
00128 return 0;
00129 }
00130 }
00131
00132
00133
00134 int pqueueConsult(PQueue pqueue,void **inf)
00135 {
00136 if(!pqueue->size)
00137 {
00138 *inf=NULL;
00139 return 1;
00140 }
00141 else
00142 {
00143 *inf=pqueue->head->inf;
00144 return 0;
00145 }
00146 }
00147
00148
00149
00150 int pqueueSize(PQueue pqueue)
00151 {
00152 return(pqueue->size);
00153 }
00154
00155
00156
00157 int pqueueMap(PQueue pqueue,void(*fun)(void*))
00158 {
00159 PQueueNode aux;
00160
00161 if(pqueue->size==0) return 1;
00162 else
00163 {
00164 for(aux=pqueue->head;aux;aux=aux->next)
00165 fun(aux->inf);
00166
00167 return 0;
00168 }
00169 }
00170
00171
00172
00173 Iterator pqueueIterator(PQueue pqueue)
00174 {
00175 int ctrl;
00176 PQueueNode aux;
00177 Iterator it;
00178
00179 it=newIt(pqueue->size);
00180 for(aux=pqueue->head,ctrl=0;aux&&!ctrl;aux=aux->next)
00181 ctrl=itAdd(it,aux->inf);
00182
00183 if(ctrl)
00184 {
00185 itDelete(it);
00186 return NULL;
00187 }
00188 else return it;
00189 }