I am implementing Priority QUE as a doubly linked list. My structs:
typedef int kintyr;
typedef struct qElem {
struct qElem *prv;
kintyr *dat;
int *priority;
}qElem;
typedef struct que {
qElem *fr,*bk;
int cnt;
}que;
And this is my functions to create empty PQ, and to insert elements:
que *qNew()
{
que *q = malloc(sizeof(*q));
if (q==NULL)
return NULL;
q->fr = NULL;
q->bk = NULL;
q->cnt = 0;
qFault = 0;
return q;
}
que *qEnq(que *q, kintyr *x, int *prrt)
{
que *zn=q;
qFault = 0;
if (q == NULL)
{
qFault = 1;
return q;
}
if (qCHKf(q) == 1)
{
qFault = 3;
return q;
}
qElem *new = malloc(sizeof(*new));
new->prv = NULL;
new->dat = x;
new->priority=prrt;
if (q->fr == NULL || q->fr->priority>prrt )
{
new->prv=q->fr;
q->fr = new;
}
else
{
que *tempas=q;
while(tempas->fr->prv!=NULL && tempas->fr->priority<=prrt)
tempas=tempas->fr;
new->prv=tempas->fr;
tempas->fr=new;
}
q->cnt++;
return q;
}
It works good if I add for example elements with priority 7, then 4, then 5.
4->5->7
But if I add element with priority 7, then 6, then 8. It appears:
6->**8**->7
Do you have any ideas how can I fix that?