Implement Enqueue, Dequeue and Display operations on circular queues maintained using arrays.
/* Circular Queue - Array */
#include
#include
#define SIZE 10
int queue[SIZE];
int front = -1;
int rear = -1;
void enqueue(void); //add element
void dequeue(void); //delete element
void dis(void);
void main()
{
int choice = 1;
while (choice == 1 || choice == 2 || choice == 3)
{
clrscr();
printf("Implementing Circular Queue Using Arrays :-\n");
printf("-------- ----- -------------- ----- ------\n\n");
printf("1] ADD an element to the queue\n");
printf("2] DELETE an element from the queue\n");
printf("3] DISPLAY the queue elements\n");
printf("4] EXIT \n\n");
printf("Enter your choice :- ");
scanf("%d",&choice);
switch (choice)
{
case 1 :
enqueue ();
break;
case 2 :
dequeue ();
break;
case 3 :
dis();
break;
case 4 :
break;
default :
printf("\nOut of range\n\n");
choice = 1;
getch();
}
}
clrscr();
printf("\nProgram finished......");
getch();
}
/* Function to add an element to the queue */
void enqueue(void)
{
int value;
if ((rear == (SIZE-1) && front == 0) || (front == (rear+1)))
{
printf("Queue Overflow...");
getch();
return;
}
else
{
printf("\nEnter the element value :- ");
scanf("%d",&value);
rear++;
if (front == -1)
front++;
if (rear > (SIZE-1))
rear = 0;
queue[rear]=value;
}
return;
}
/* Function to remove an element from the queue */
void dequeue(void)
{
int value;
if (front == -1)
{
printf("\nQueue empty...");
getch();
return;
}
else
{
value = queue[front];
queue[front] = 0;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
if (front > (SIZE-1))
front = 0;
}
}
printf("\n%d queue element removed from the queue...\n",value);
return;
}
/* Display all the elements of the queue */
void dis(void)
{
int i;
printf("\nFront = %d, Rear = %d\n",front, rear);
if (front == -1)
{
printf("\nQueue is empty...");
getch();
return;
}
if (rear >= front)
{
for (i=front; i<=rear;i++)
{
printf("%d ----> %d\n",i,queue[i]);
}
}
else
{
for (i=front;i<=(SIZE-1);i++)
{
printf("%d ----> %d\n",i,queue[i]);
}
for (i=0;i<=rear;i++)
{
printf("%d ----> %d\n",i,queue[i]);
}
}
getch();
return;
}
OUTPUT:-
Implementing Circular Queue Using Arrays :-
-------- ----- -------------- ----- ------
1] ADD an element to the queue
2] DELETE an element from the queue
3] DISPLAY the queue elements
4] EXIT
Enter your choice :- 1
Enter the element value :- 11
Enter your choice :- 1
Enter the element value :- 22
Enter your choice :- 1
Enter the element value :- 33
Enter your choice :- 1
Enter the element value :- 44
Enter your choice :- 3
Front=0, Rear=3
0 ----> 11
1 ----> 22
2 ----> 33
3 ----> 44
Enter your choice :- 4
Program Finished . . . .
/* Circular Queue - Array */
#include
#include
#define SIZE 10
int queue[SIZE];
int front = -1;
int rear = -1;
void enqueue(void); //add element
void dequeue(void); //delete element
void dis(void);
void main()
{
int choice = 1;
while (choice == 1 || choice == 2 || choice == 3)
{
clrscr();
printf("Implementing Circular Queue Using Arrays :-\n");
printf("-------- ----- -------------- ----- ------\n\n");
printf("1] ADD an element to the queue\n");
printf("2] DELETE an element from the queue\n");
printf("3] DISPLAY the queue elements\n");
printf("4] EXIT \n\n");
printf("Enter your choice :- ");
scanf("%d",&choice);
switch (choice)
{
case 1 :
enqueue ();
break;
case 2 :
dequeue ();
break;
case 3 :
dis();
break;
case 4 :
break;
default :
printf("\nOut of range\n\n");
choice = 1;
getch();
}
}
clrscr();
printf("\nProgram finished......");
getch();
}
/* Function to add an element to the queue */
void enqueue(void)
{
int value;
if ((rear == (SIZE-1) && front == 0) || (front == (rear+1)))
{
printf("Queue Overflow...");
getch();
return;
}
else
{
printf("\nEnter the element value :- ");
scanf("%d",&value);
rear++;
if (front == -1)
front++;
if (rear > (SIZE-1))
rear = 0;
queue[rear]=value;
}
return;
}
/* Function to remove an element from the queue */
void dequeue(void)
{
int value;
if (front == -1)
{
printf("\nQueue empty...");
getch();
return;
}
else
{
value = queue[front];
queue[front] = 0;
if (front == rear)
{
front = -1;
rear = -1;
}
else
{
front++;
if (front > (SIZE-1))
front = 0;
}
}
printf("\n%d queue element removed from the queue...\n",value);
return;
}
/* Display all the elements of the queue */
void dis(void)
{
int i;
printf("\nFront = %d, Rear = %d\n",front, rear);
if (front == -1)
{
printf("\nQueue is empty...");
getch();
return;
}
if (rear >= front)
{
for (i=front; i<=rear;i++)
{
printf("%d ----> %d\n",i,queue[i]);
}
}
else
{
for (i=front;i<=(SIZE-1);i++)
{
printf("%d ----> %d\n",i,queue[i]);
}
for (i=0;i<=rear;i++)
{
printf("%d ----> %d\n",i,queue[i]);
}
}
getch();
return;
}
OUTPUT:-
Implementing Circular Queue Using Arrays :-
-------- ----- -------------- ----- ------
1] ADD an element to the queue
2] DELETE an element from the queue
3] DISPLAY the queue elements
4] EXIT
Enter your choice :- 1
Enter the element value :- 11
Enter your choice :- 1
Enter the element value :- 22
Enter your choice :- 1
Enter the element value :- 33
Enter your choice :- 1
Enter the element value :- 44
Enter your choice :- 3
Front=0, Rear=3
0 ----> 11
1 ----> 22
2 ----> 33
3 ----> 44
Enter your choice :- 4
Program Finished . . . .