2014-03-12 3 views
0

이중 연결 목록으로 Priority QUE를 구현하고 있습니다. 내 구조체 :연결된 목록 정렬 (ADT Priority Que)

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 예를 요소에 추가하는 경우 그런 다음, 좋은 일 그때 우선 순위 7, 6 요소를 추가 5.

4->5->7 

, 그때 8.가 나타납니다

6->8->7 

어떻게 해결할 수 있습니까?

답변

0

q-> fr을 q로 바꿉니다. 아래 코드를 변경하십시오.

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

    }   
관련 문제