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?

Recommended Answers

All 2 Replies

Can't really check ... since some stuff missing ... too many problems to compile ...

Turn all your compiler errors / warnings on ...

See the changes / comments here / compiler warnings here

they may help ?

... may get you stated on debugging your code ?

    #include <stdio.h>
    #include <stdlib.h>

    typedef int kintyr;

    typedef struct 
    {
        int qFault;
    } Fault ;

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


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



    que *qNew( Fault* f )
    {
        que *q = malloc(sizeof( que )); /* fixed ??? */

        if (q==NULL)
            return NULL;

        q->fr = NULL;
        q->bk = NULL;
        q->cnt = 0;


        /* qFault = 0; // what is this ... not used here anyways */
        f->qFault = 0;

        return q;
    }

    que *qEnq(que *q, kintyr *x, int *prrt, Fault* f )
    {
        qElem* new;

        /* que *zn=q; // NOT use here ??? // */
        /* qFault = 0; */
        f->qFault = 0;
        if (q == NULL)
        {
            /* qFault = 1; */
            f->qFault = 1;
            return q;
        }
        /* // can't check this WITHOUT having function qCHK 
        if (qCHKf(q) == 1)
        {
            // qFault = 3; //
            f.qFault = 3;

            return q;
        }
        */
        /* qElem *new = malloc(sizeof(*new)); */
        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;

    }

    int main()
    {
        Fault f;
        f.qFault = 0;
        printf( "f.qFault value is %d\n", f.qFault );


        printf( "\nPress 'Enter' to continue/ecit ... " );
        getchar();

        return (0);
    }


    /*

    C:\Users\dwzavitz\Desktop\2014Files\Dani\C\priortyQue.c: In function 'qEnq':

    C:\Users\dwzavitz\Desktop\2014Files\Dani\C\priortyQue.c:74:17: 
    warning: assignment from incompatible pointer type [enabled by default]

    C:\Users\dwzavitz\Desktop\2014Files\Dani\C\priortyQue.c:82:20: 
    warning: assignment from incompatible pointer type [enabled by default]

    C:\Users\dwzavitz\Desktop\2014Files\Dani\C\priortyQue.c:84:17: 
    warning: assignment from incompatible pointer type [enabled by default]

    Execution terminated
    Compilation successful

    */

Thanks for your post, I know that it's hard to read code without all program. I found the mistake:
It should be

while(tempas->fr!=NULL && tempas->fr->priority<=prrt)
                 tempas=tempas->fr;

And not:

while(tempas->fr**->prv**!=NULL && tempas->fr->priority<=prrt)
                 tempas=tempas->fr;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.