I had forgotten to add a temporary node in the beginning in the display() method but later I added it, still, I am facing the same issue. I was unable to find the problem in my code.
I have added the complete code below:
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int key;
struct Node* next;
};
struct Queue
{
struct Node *front, *rear;
};
struct Node* newNode(int k)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->key = k;
temp->next = NULL;
return temp;
}
struct Queue* createQueue()
{
struct Queue* q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}
void enQueue(struct Queue* q, int k)
{
struct Node* temp = newNode(k);
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}
q->rear->next = temp;
q->rear = temp;
}
void deQueue(struct Queue* q)
{
if (q->front == NULL)
{
printf("The Queue is empty.\n");
return;
}
struct Node* temp = q->front;
printf("Deleted Element: %d\n",q->front->key);
q->front = q->front->next;
if (q->front == NULL)
q->rear = NULL;
free(temp);
}
void display(struct Queue* q)
{
struct Queue* temp=q;
if(temp->front == NULL)
{
printf("The Queue is Empty.\n");
}
else
{
printf("Queue contains the following elements:\n");
while(temp->front != NULL)
{
printf("%d\t",temp->front -> key);
temp->front = temp->front -> next;
}
printf("\n");
}
}
int main()
{
struct Queue* q = createQueue();
int choice=0;
while(choice != 4)
{
printf("Choose from the following by entering the index number:\n1.Insert an element\n2.Delete an element\n3.Display the elements in queue\n4.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
int k;
printf("Enter the element you want to insert: ");
scanf("%d",&k);
enQueue(q,k);
break;
}
case 2:
{
deQueue(q);
break;
}
case 3:
{
display(q);
break;
}
case 4:
{
printf("\nThe program has ended.");
exit(0);
break;
}
default:
{
printf("\nInvalid Input! Please try again.");
}
}
}
}
CodePudding user response:
In your display
function, what do you think this line of code does?
temp->front = temp->front -> next;
It's moving the front of your queue forwards until it ends up pointing to NULL
. Because you're not making another copy of the queue by assigning q
to temp
- they both point to the same queue.
Instead, what you want to do is make temp
a struct Node
and move that along the queue - something like this
void display(struct Queue* q)
{
struct Node* temp=q->front;
if(temp == NULL)
{
printf("The Queue is Empty.\n");
}
else
{
printf("Queue contains the following elements:\n");
while(temp != NULL)
{
printf("%d\t",temp -> key);
temp = temp -> next;
}
printf("\n");
}
}
CodePudding user response:
For starters an allocation of memory for a new mode can fail. In this case your code will invoke undefined behavior. Rewrite the function newNode
the following way
struct Node * newNode( int k )
{
struct Node *temp = malloc( sizeof( struct Node ) );
if ( temp != NULL )
{
temp->key = k;
temp->next = NULL;
}
return temp;
}
Correspondingly the function enQueue
should be rewritten the following way
int enQueue( struct Queue *q, int k )
{
struct Node *temp = newNode( k );
int success = temp != NULL;
if ( success )
{
if ( q->front == NULL)
{
q->front = temp;
}
else
{
q->rear->next = temp;
}
q->rear = temp;
}
return success;
}
The function deQueue
should not issue any message. It is the caller of the function that decides whether to issue a message.
You could write one more function that reports whether the queue is empty. For example
int isEmpty( const struct Queue *q )
{
return q->front == NULL;
}
The function deQueue
can be defined the following way
void deQueue( struct Queue *q )
{
if ( !isEmpty( q )
{
struct Node *temp = q->front;
q->front = q->front->next;
if ( q->front == NULL ) q->rear = NULL;
free( temp );
}
}
And the function can be called like
case 2:
{
if ( isEmpty( q ) )
{
printf("The Queue is empty.\n");
}
else
{
printf("Deleted Element: %d\n",q->front->key);
deQueue( q );
}
}
Within the function display
after this assignment
struct Queue* temp=q;
the pointer temp
points to the same object of the type struct Queue as the pointer q. That is you are accessing the same object using two different pointers. So using the pointer temp
you are changing the original object of the type struct Queue
temp->front = temp->front -> next;
So after this loop
while(temp->front != NULL)
the pointer front
of the original object of the type struct Queue will be equal to NULL.
The function can be defined the following way
void display( const struct Queue *q )
{
printf("Queue contains the following elements:\n");
for ( const struct Node *current = q->front; current != NULL; current = current->next;)
{
printf( "%d\t", current->key);
}
printf("\n");
}
And the function can be called like
case 3:
{
if ( isEmpty( q ) )
{
printf( "The Queue is Empty.\n" );
}
else
{
display( q );
}
}