2014-03-12 64 views
0

我正在实施优先级QUE作为双向链表。 我的结构:排序链接列表(ADT优先级排序)

typedef int kintyr; 

typedef struct qElem { 
    struct qElem *prv;   
    kintyr *dat;      
    int *priority; 
}qElem; 


typedef struct que { 
    qElem *fr,*bk;    
    int cnt;      
}que; 

这是我的函数来创建空的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; 
} 

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; 

} 

如果我添加例如元素与优先级为7,然后4它的工作好,然后5.

4->5->7 

我若优先级为7,然后添加6元件,然后8.看来:

6->8->7 

你有什么想法,我该如何解决这个问题?

回答

0

将q-> fr替换为q。所以改变下面的代码。

if (q == NULL || q->priority>prrt ) 
    { 
     new->prv=q; 
     q = new; 

    }