-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.c
120 lines (112 loc) · 2.07 KB
/
circularqueue.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<stdio.h>
#include <stdlib.h>
#define MAX 5
int front=-1, rear=-1,q[MAX];
void enqueue(int num)
{
if(front==(rear+1)%MAX)
{
printf("queue is full");
}
else if(front==-1)
{
rear=front=0;
q[rear]=num;
printf("the enqueued element is %d",q[rear]);
}
else
{
rear=(rear+1)%MAX;
q[rear]=num;
printf("the enqueued element is %d",q[rear]);
}
}
void dequeue()
{
if(front==-1)
{
printf("the queue is empty");
}
else if(rear==front)
{
int temp=q[front];
rear=front=-1;
printf("%d is dequeued",temp);
}
else
{
int temp=q[front];
front=(front+1)%MAX;
printf("%d is dequeued",temp);
}
}
void peek()
{
if(front==-1)
{
printf("the queue is empty");
}
else
{
printf("the element in the front is%d",q[front]);
}
}
void display()
{
if(front==-1)
{
printf("the queue is empty");
}
else
{
printf("the current queue is:");
int i=front;
while(1)
{
printf("%d\n",q[i]);
if(i==rear)
break;
i=(i+1)%MAX;
}
}
}
void main()
{
int ch,num;
while(1)
{
printf("\nchoose from menu\n1.enqueue\n2.dequeue\n3.peek\n4.display\n5.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("enter the number to be enqueued");
scanf("%d",&num);
enqueue(num);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
case 5:
{
exit(1);
break;
}
default:printf("wrong choice");
}
}
}