Implementing a queue with linked lists in C

The following is an implementation of my queue, which has the functions of a queue and a queue from a queue. For some reason, it crashes without prompting (where it crashes), since the code works on android. I suspect my queue code. If you guys know what is wrong with my code, please give me an idea.

Thanks for any help.

Here is my C code:

    int qLast = 0;

typedef struct phoneSt PhoneStructure;

typedef struct{
    PhoneStructure Phone;
    struct phoneQ *next;
}phoneQ;


phoneQ *headElement = NULL;    /* front pointer in queue*/
phoneQ *tailElement = NULL;     /* rear pointer in queue */

void enqueue_Phone(PhoneStructure Frame)
{
    phoneQ *newnode;      /* New node to be inserted */
    newnode=(phoneQ*)av_malloc(sizeof(phoneQ));
    newnode->next=NULL;
    newnode->Phone=Frame;
        qLast++;
    if(headElement==NULL && tailElement==NULL)
    {
        headElement=newnode;
        tailElement=newnode;
    }
    else
    {
        tailElement->next=newnode;
        tailElement=newnode;
                                                                                                                   }
        __android_log_print(ANDROID_LOG_DEBUG, "myphone.c", "Queue is having %d element", qLast);
}

PhoneStructure dequeue_Phone()
{
    phoneQ *delnode;      /* Node to be deleted */
    PhoneStructure Frame;
        __android_log_write(ANDROID_LOG_DEBUG, "myplayer.c", "In dequeue_Phone");
    if(headElement==NULL && tailElement==NULL){
        __android_log_write(ANDROID_LOG_ERROR, "myphone.c", "Queue is empty to delete any element");
        }
    else
    {
        __android_log_write(ANDROID_LOG_DEBUG, "myphone.c", "In dequeue  queue is not empty");
        delnode=headElement;
        headElement=headElement->next;
        Frame = delnode->Phone;
        av_free(delnode);
        qLast--;
    }
        __android_log_print(ANDROID_LOG_DEBUG, "myphone.c", "In dequeue_Phone returning  remaining  %d",qLast);
        return Frame;
}
+3
source share
2 answers

, tailElement NULL. , , headElement , , . , , headElement- > next, .

...
headElement=headElement->next;
if (!headElement)
    tailElement=NULL;
Frame = delnode->Phone;
...
+4

, , head- > next == NULL . , , NULL, node . , .

0

Source: https://habr.com/ru/post/1792629/


All Articles