forked from amanvar/Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircular queue.cpp
More file actions
122 lines (109 loc) · 2.13 KB
/
circular queue.cpp
File metadata and controls
122 lines (109 loc) · 2.13 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
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
#include<iostream>
#include<conio.h>
using namespace std;
class queue1
{
int n=8,front1=-1,rear=-1,a=0;
public:
int arr[8];
void element()
{
cout<<"enter element";
cin>>a;
}
void inqueue()
{
if((front1>=0&&rear==n-1)||(front1==rear+1))
{
//overflow
cout<<"queue overflow"<<endl;
}
if (front1==-1&&rear==-1)
{
//queue empty
front1=0;
rear=0;
arr[rear]=a;
}
else if(rear==n-1)
{
rear=0;
arr[rear]=a;
}
else {
rear++;
arr[rear]=a;
}
}
int b;
void dequeue()
{
if(rear==-1&&front1==-1)
{
cout<<"underflow"<<endl;
}
if (front1==rear)
{
//one element
b=arr[front1];
cout<<"deleted single element is :"<<b<<endl;
front1=-1;
rear=-1;
}
else if (front1==n-1)
{
b=arr[front1];
cout<<"deleted element last is :"<<b<<endl;
front1=0;
}
else
{
b=arr[front1];
cout<<"deleted any element is :"<<b<<endl;
front1++;
}
}
void display()
{
cout<<"the no. in queue are"<<endl;
for(int i=front1;i<=rear;i++)
{
cout<<arr[i]<<endl;
}
}
};
int main()
{
queue1 q;
int ch,c=1;
while(c==1)
{
cout<<"enter choice 1.insertion 2.deletion 3.display 4.exit"<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
q.element();
q.inqueue();
break;
}
case 2:
{
q.dequeue();
break;
}
case 3:
{
q.display();
break;
}
case 4:
{
c=0;
break;
}
}
}
}
//seraching n traversing in lkd list