A
Alessandro Zucchini
does someone can understand this function?can you explain me what do they do
one by one?thankyou
PQUEUE *pqueue_new(void)
{
PQUEUE *pq;
if ((pq = (PQUEUE *) my_malloc(sizeof(PQUEUE))) == NULL)
return (NULL);
pq->count = 0;
pq->priorities = NULL;
return pq;
}
int pqueue_insert(PQUEUE * pq, COORD coord, int wt)
{
ELIST *e;
PLIST *p, *pp;
if (pq == NULL)
return -1;
if ((e = (ELIST *) my_malloc(sizeof(ELIST))) == NULL)
return -1;
e->coord = coord;
e->next = NULL;
if (pq->priorities == NULL || pq->priorities->wt > wt) {
if ((p = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;
p->wt = wt;
p->elements = e;
p->next = pq->priorities;
pq->priorities = p;
pq->count++;
return 0;
}
pq->count++;
for (p = pq->priorities; p->next && p->next->wt <= wt; p = p->next);
if (p->wt == wt) {
e->next = p->elements;
p->elements = e;
return 0;
}
if ((pp = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;
pp->wt = wt;
pp->elements = e;
pp->next = p->next;
p->next = pp;
pq->count++;
return 0;
}
int pqueue_peekmin(PQUEUE * pq, PCOORD coord)
{
if (pq == NULL || pq->priorities == NULL || pq->priorities->elements ==
NULL)
return -1;
*coord = pq->priorities->elements->coord;
return 0;
}
int pqueue_popmin(PQUEUE * pq, PCOORD coord)
{
ELIST *tmpe;
PLIST *tmpp;
if (pqueue_peekmin(pq, coord) < 0)
return -1;
pq->count--;
tmpe = pq->priorities->elements;
pq->priorities->elements = pq->priorities->elements->next;
my_free(tmpe, sizeof(ELIST));
if (pq->priorities->elements == NULL) {
tmpp = pq->priorities;
pq->priorities = pq->priorities->next;
my_free(tmpp, sizeof(PLIST));
}
return 0;
}
one by one?thankyou
PQUEUE *pqueue_new(void)
{
PQUEUE *pq;
if ((pq = (PQUEUE *) my_malloc(sizeof(PQUEUE))) == NULL)
return (NULL);
pq->count = 0;
pq->priorities = NULL;
return pq;
}
int pqueue_insert(PQUEUE * pq, COORD coord, int wt)
{
ELIST *e;
PLIST *p, *pp;
if (pq == NULL)
return -1;
if ((e = (ELIST *) my_malloc(sizeof(ELIST))) == NULL)
return -1;
e->coord = coord;
e->next = NULL;
if (pq->priorities == NULL || pq->priorities->wt > wt) {
if ((p = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;
p->wt = wt;
p->elements = e;
p->next = pq->priorities;
pq->priorities = p;
pq->count++;
return 0;
}
pq->count++;
for (p = pq->priorities; p->next && p->next->wt <= wt; p = p->next);
if (p->wt == wt) {
e->next = p->elements;
p->elements = e;
return 0;
}
if ((pp = (PLIST *) my_malloc(sizeof(PLIST))) == NULL)
return -1;
pp->wt = wt;
pp->elements = e;
pp->next = p->next;
p->next = pp;
pq->count++;
return 0;
}
int pqueue_peekmin(PQUEUE * pq, PCOORD coord)
{
if (pq == NULL || pq->priorities == NULL || pq->priorities->elements ==
NULL)
return -1;
*coord = pq->priorities->elements->coord;
return 0;
}
int pqueue_popmin(PQUEUE * pq, PCOORD coord)
{
ELIST *tmpe;
PLIST *tmpp;
if (pqueue_peekmin(pq, coord) < 0)
return -1;
pq->count--;
tmpe = pq->priorities->elements;
pq->priorities->elements = pq->priorities->elements->next;
my_free(tmpe, sizeof(ELIST));
if (pq->priorities->elements == NULL) {
tmpp = pq->priorities;
pq->priorities = pq->priorities->next;
my_free(tmpp, sizeof(PLIST));
}
return 0;
}