C/Pointer's Implementation of Queue in C, not working!
Expert: Narendra - 4/21/2006
Questioni've written a program in C for pointer's implementation of Queue
it doesnt seem to work properly, here are the problems
1. when the q is empty and i try to dequeue it's suppose to print "Dequeue is not possible, Queue is underflow" but
instead it's printing some garbage value.
2. when the q is full, when it contains 5 elements, after deleting an element from the q (so that now q has 4 elements)
it doesnt allow me to Enqueue, basically once the queue reaches maximum i cannot insert anymore elements unless
THE WHOLE QUEUE is EMPTY!
i've understand the concepts of queues but it's the pointer's implemenation which is annoying me, and since all elements are
lined up in an array, C has a way of starting the array with a[0].
please help me make the program work, hope im not askin for too much.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
void qinsert(int q[], int *front, int *rear)
{
int item;
if(*rear==size-1)
{
printf("\nQueue Overflow");
}
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d",&item);
if(*front==1)
{
*front=0;
*rear=0;
}
else
(*rear)++;
q[*rear]=item;
}
}
void qdelete(int q[], int *front, int *rear)
{
int item;
if(*front==-1)
printf("\nQueue Underflow");
else
{
item=q[*front+1];
printf("\nThe deleted item is: %d", item);
if(*front==*rear)
{
*front=-1;
*rear=-1;
}
else
(*front)++;
}
}
void display(int q[], int *front, int *rear)
{
int i;
if(*rear==-1)
{
printf("\nQueue is empty");
}
else
for(i=*front+1;i<=*rear;i++)
printf("\n%d",q[i]);
}
void main()
{
int q[size], front=-1, rear=-1, choice;
clrscr();
do
{
printf("\n1.Insertion");
printf("\n2.Deletion");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
qinsert(q,&front,&rear);
break;
case 2:
qdelete(q,&front,&rear);
break;
case 3:
display(q,&front,&rear);
break;
}
}
while(choice!=4);
getch();
}
AnswerI am exetremely sorry for the delay in replying.
I was held up with some important work and also my telephone line is down!
Here is the corrected code and you can test it with your inputs.
In case, you hit some problem, please let me know:
#include<stdio.h>
#include<stdlib.h>
#define size 5
void qinsert(int q[], int *front, int *rear)
{
int item;
if (abs(*rear - *front) == (size - 1))
{
printf("\nQueue Overflow");
}
else
{
printf("\nEnter the element to be inserted: ");
scanf("%d", &item);
(*rear)++;
q[*rear] = item;
}
}
void qdelete(int q[], int *front, int *rear)
{
int item;
if (*front == *rear)
printf("\nQueue Underflow");
else
{
item = q[*front + 1];
printf("\nThe deleted item is: %d", item);
if (*front == *rear)
{
*front = -1;
*rear = -1;
}
else
(*front)++;
}
}
void display(int q[], int *front, int *rear)
{
int i;
if(*rear==-1)
{
printf("\nQueue is empty");
}
else
for(i= *front + 1;i <= *rear;i++)
printf("\n%d" ,q[i]);
}
main()
{
int q[size], front=-1, rear=-1, choice;
do
{
printf("\n1.Insertion");
printf("\n2.Deletion");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
qinsert(q, &front, &rear);
break;
case 2:
qdelete(q, &front, &rear);
break;
case 3:
display(q, &front, &rear);
break;
}
}
while(choice !=4 );
}