0

I have a problem with my program. I have created a queue of linked lists, and when I clear my queue with my delQueue function, my queue disappears, and I can't push anything in anymore.

How can I fix this? My push function works fine unless I delete everything from my queue.

Here is my code:

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

int count = 0;

struct Node
{
    int Data;
    struct Node* next;
}*rear, *front;

void delQueue()
{

    struct Node *var=rear;
    while(var!=NULL)
    {
        struct Node* buf=var->next;
        free(var);
        count = count + 1;

    }

}

void push(int value)
{
    struct Node *temp;
    temp=(struct Node *)malloc(sizeof(struct Node));
    temp->Data=value;
    if (front == NULL)
    {
        front=temp;
        front->next=NULL;
        rear=front;
    }
    else
    {
        front->next=temp;
        front=temp;
        front->next=NULL;
    }
}

void display()
{
    struct Node *var=rear;
    if(var!=NULL)
    {
        printf("\nElements in queue are:  ");
        while(var!=NULL)
        {
            printf("\t%d",var->Data);
            var=var->next;
        }
    printf("\n");
    } 
    else
    printf("\nQueue is Empty\n");
}
3
  • 1
    when you push you are updating front. Should not rear be updated when you are pushing? Queue is first in first out...so when you push, rear pointer should be updated. Commented May 4, 2013 at 2:16
  • Thank you so much everyone you have been very helpful. I really appreciate all the help that I have recieved here and I have surely learned a lot about queues. than you again. God bless u all Commented May 4, 2013 at 2:43
  • you should accept the answer which you think best answered your question ;) Commented May 4, 2013 at 2:51

4 Answers 4

0
void delQueue()
{
    while(rear != NULL) {
        struct Node* var=rear->next;
        free(rear);
        count = count + 1;
        rear = var;         /* update rear */
    }
    front = NULL; /* clear the front */
}
Sign up to request clarification or add additional context in comments.

3 Comments

My queue now exists but I can't add anything it won't push anything in it but the queue is there and empty.
See the update, maybe it is because you are not resetting the front.
Since it is a queue, I prefer deleting from front, as given in my answer. Otherwise you loose the meaning of a queue, FIFO
0

You are looking at "var" after you freed it (when you go around the loop again). Did you mean to assign "var = buf" in the loop in delQueue()?

Also, don't forget to check the return from malloc() for NULL in your push() routine. Even if this is just a small learning program, you should learn to always check...

Comments

0
 int delQueue()
    {
   int count = 0;
    while ( front != NULL )
    {
    struct Node * temp = front;
    front = front -> next;
    free (temp);
    count++;
    }
    rear= NULL;

    return count;
    }

Since it is a queue, I would prefer to delete elements from the front, rather than rear.

1 Comment

it works perfect but I can't seem to keed the count of how many items I delete. Where will I put that??
0

you have to add the following line at the end of delQueue()

rear = front = NULL;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.