#include<stdio.h>
#include<stdlib.h>
#define MAX 5
char queue[MAX][30];
int front, rear, temp;
void init()
{
front = -1;
rear = -1;
}
void insert()
{
if ( rear >= MAX-1 && front == -1 )
{
printf("\nQueue overflow");
return ;
}
temp = rear;
rear = ( rear + 1 ) % MAX ;
printf( "\nEnter name to be inserted :\n") ;
scanf( "%s", queue[rear]) ;
}
void delete()
{
if (front == rear || rear == -1)
printf("\nError : Underflow");
else {
front = (front + 1) % MAX;
printf("\nDeleted name from the CQ is %s",queue[front]);
}
}
void display()
{
int i;
printf("\nThe queue content is :\n");
if (front > rear)
{
for (i = front+1 ; i < MAX; i++)
printf("%s\t",queue[i]);
for (i = 0; i <= rear; i++)
printf("%s\t",queue[i]);
}
else
for (i = front +1 ; i <= rear; i++)
printf("%s\t",queue[i]);
printf("\n");
}
void front_rear()
{
printf("Front = %d \n", front);
printf("Rear = %d \n", rear);
}
int main()
{
int choice;
init();
while(1)
{
printf("\nMENU\n1. Insert\n2. Delete\n3. Display\n4. Exit\n5.front_rear_pos\n");
printf("\nYour choice = ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
if (rear==front)
printf("\nNo elements in the list");
else
display();
break;
case 4:
exit(0);
case 5:
front_rear();
break;
default:
printf("Invalid Choice");
break;
}
}
return 0;
}
i have written a code for circular Queue using array.
the probelm with this is :
1. when i enter all the 5 elements
the rear value is 4 and front value is -1 if i dont delete any element.
it( insert function) works fine .
10 20 30 40 50 are intial elements
here rear = 4 and front = -1;
then i deteted one element
now display works fine : 20 30 40 50 ( rear = 4 anf front = 0)
as its a circular queue the program should allow me to enter the number and store in the zero position as one element is deleted.
but when i display the list after inserting an element , it displays no elements.
i tried my level best but could not find the correct control statement .
i want my circular queue to be flexible .
when i delete an element it should allow me to store the value there and display all the elements including the previous elements.
plz sugget me how to solve it.
thanks,
Danian