-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcircularq_array.c
117 lines (106 loc) · 1.97 KB
/
circularq_array.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
#include <stdio.h>
#define MAX 5
int queue[MAX], front = -1, rear = -1;
int isEmpty()
{
return(front == -1);
}
// Push function to insert element
void push(int data)
{
if( (front == 0 && rear == MAX - 1) ||
(rear == front-1)
)
printf("\n Queue overflow");
else if(front == -1)
{
front = 0;
rear = 0;
queue[rear] = data;
}
else
{
rear = ++rear%MAX;
queue[rear] = data;
}
}
// Pop function to pop element
void pop()
{
int del;
if (front == -1)
printf("\nQueue empty");
else if(front == rear)
{
printf("\nDeleted: %d", queue[front]);
front = -1;
rear = -1;
}
else
{
del = queue[front];
printf("\nDeleted: %d", del);
front = ++front%MAX;
}
}
// Displays front element without popping it
int peek()
{
if(front == -1)
printf("\nQueue empty");
else
printf("\n Front: %d", queue[front]);
}
// To print elements of queue
void display()
{
if (front== -1)
printf("\nQueue empty");
else if(front <= rear)
{
printf("\n");
for (int i = front; i <= rear; i++)
{
printf("%d, ", queue[i]);
}
}
else
{
for(int i=front; i<MAX; i++)
printf("%d, ", queue[i]);
for(int i=0; i<=rear; i++)
printf("%d, ", queue[i]);
}
}
int main()
{
int ch, element;
do
{
printf("\n\nChoose operation");
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display queue");
printf("\n4.Display Front Element");
printf("\n5.Exit\n");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter element to insert ");
scanf("%d", &element);
push(element);
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
}
} while (ch < 5);
return 0;
}