问题
I am implementing Abstract Data Type - Priority que, but I can't find out how to put values in correct order. My structures:
typedef int kintyr;
typedef struct qElem {
struct qElem *prv;
kintyr *dat;
int *priority;
}qElem;
typedef struct que {
qElem *fr,*bk;
int cnt;
}que;
And now my main functions to implement a Priority que
First to create an empty PQ:
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;
}
This is function to add elements, but it isnt working
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 || prrt < q->fr->priority )
{
q->fr = new;
}
else
{
que *temp=q;
while(temp->fr!=NULL&&q->fr->priority <= prrt)
temp = temp->fr;
new->prv=temp->fr;
temp->fr=new;
//if (temp->bk != NULL)
//q->bk->prv = new;
q->bk = new; }}
q->cnt++;
return q;
}
}
Do you have any thoughts how can i fix this?
来源:https://stackoverflow.com/questions/22337289/implementing-adt-priority-que-as-linked-list-cant-sort-elements