-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path04-Circular Queue (Array).c
More file actions
77 lines (67 loc) · 1.12 KB
/
04-Circular Queue (Array).c
File metadata and controls
77 lines (67 loc) · 1.12 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<stdio.h>
#include<stdlib.h>
#define size 5
int a[size], front=0, rear=0, n;
void display ()
{
int i;
printf ("\nThe queue is:\n");
if (front==rear)
printf ("empty\n");
else
{
for (i = front+1; i != rear; i = (i + 1) % n)
printf ("%d ", a[i]);
printf ("%d\n", a[i]);
}
}
void push()
{
if(front==(rear+1)%n)
printf("Queue overflow\n");
else
{
rear=(rear+1)%n;
printf("\nEnter the element to push\n");
scanf("%d", &a[rear]);
display();
}
}
void pop()
{
if(front==rear)
printf("Queue underflow\n");
else
{
printf("\nPopped element %d from queue\n", a[front+1]);
if(rear==1)
front=rear=0;
else
front=(front+1)%n;
display();
}
}
void main()
{
int choice;
printf("Enter the size of circular queue\n");
scanf("%d", &n);
while(1)
{
printf("\nCircular Queue\n______________\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit \n");
printf("\nEnter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("Invalid choice\n");
}
}
}