forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.cpp
127 lines (120 loc) · 1.67 KB
/
circularqueue.cpp
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
121
122
123
124
125
126
127
#include <stdio.h>
int rear = -1;
int n = 5;
int front = n;
int insert(int arr[], int num)
{
if (front == 0)
{
printf("Overflow\n\n");
}
else
{
arr[n - 2 - rear] = num;
rear++;
front--;
printf("The number inserted is %d\n\n", num);
}
}
int del(int arr[])
{
if (rear == -1)
{
printf("UnderFlow\n\n");
}
else
{
printf("The deleted number is %d\n\n", arr[n - 1]);
for (int i = n - 1; i >= front; i--)
{
arr[i] = arr[i - 1];
}
front++;
rear--;
}
}
bool present = false;
int x = 0;
int peep(int arr[], int m)
{
if (rear == -1)
{
printf("Queue is empty\n\n");
}
else
{
for (int i = front; i < n; i++)
{
if (arr[i] == m)
{
present = true;
x = i - front;
break;
}
}
if (present)
{
printf("\nIt is present at index %d\n\n", x);
}
else
{
printf("\nIt is not present in the queue.\n\n");
}
}
}
int display(int arr[])
{
if (rear == -1)
{
printf("Queue is Empty\n\n");
}
else
{
for (int i = front; i < n; i++)
{
printf("%d ", arr[i]);
}
printf("\n\n");
}
}
int main()
{
int arr[n];
int in;
int num;
while (in != -1)
{
printf("Enter 0 for insert, 1 for delete , 2 for peep, 3 for display:");
scanf("%d", &in);
if (in == 0)
{
printf("Enter the number you want to insert:");
scanf("%d", &num);
insert(arr, num);
}
if (in == 1)
{
del(arr);
}
if (in == 2)
{
printf("Enter the number you want to peep:");
scanf("%d", &num);
peep(arr, num);
}
if (in == 3)
{
display(arr);
}
}
return 0;
// insert(arr,100);
// insert(arr,20);
// insert(arr,30);
// insert(arr,40);
// insert(arr,2);
// del(arr);
// insert(arr,88);
// display(arr);
// peep(arr,99);
}