-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_queue.c
70 lines (61 loc) · 1.38 KB
/
circular_queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<stdio.h>
int a[10];
void main()
{
int n,i,item,ch;
printf("Enter the number of elements:");
scanf("%d",&n);
int front=-1,rear=-1;
do
{
printf("\n1. ADD 2.DELETE 3.DISPLAY 4.EXIT");
printf("Enter the choice :");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element:");
scanf("%d",&item);
if( (front == rear + 1) || (front == 0 && rear == n-1))
printf("The Queue is full");
else
{
if(front == -1) front = 0;
rear = (rear + 1)%n;
a[rear] = item;
printf("\n Inserted -> %d", item);
}
break;
case 2:
if(front == -1)
printf("\nCircular Queue is Empty! Deletion is not possible!!!\n");
else{
item = a[front];
if (front == rear){
front = -1;
rear = -1;
}
else {
front = (front + 1)%n;
}
printf("\n Deleted element -> %d \n", item);
}
break;
case 3: if(front == -1)
printf(" \n Empty Queue\n");
else
{
printf("Enter the element in Queue are:\n");
for( i = front; i!=rear; i=(i+1)%n)
{
printf("%d ",a[i]);
}
printf("%d ",a[i]);
}
break;
case 4: printf("Exiting from Queue\n");
break;
default: printf("\nInvalid choice");
break;
}
}while(ch!=4);
}