Implementing ADT Priority Que as linked list, can't sort elements

半世苍凉 提交于 2019-12-13 04:28:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!