-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathinsertion and deletion in Queue.cpp
89 lines (89 loc) · 1.62 KB
/
insertion and deletion in Queue.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
#include <iostream>
using namespace std;
int front=-1;int rear=-1;
int var;
void enqueue(int queue[],int size,int x)
{
if(rear==size-1)
cout<<"Overflow\n";
else if(front==-1&&rear==-1)
{
front=0;
rear=0;
}
else
rear++;
queue[rear]=x;
}
int dequeue(int queue[],int size)
{
if(front==-1)
{
cout<<"Underflow\n";
return-1;
}
else if(front==rear)
{
var=queue[front];
front=-1;
rear=-1;
}
else
{
var=queue[front];
front++;
}
return var;
}
void display(int queue[],int size)
{
if(front==-1){
cout<<"Queue is empty\n";
}
else
{
cout<<"Elements are:";
for(int i=front;i<=rear;i++)
{
cout<<queue[i]<<" ";
}
cout<<"\n";
}
}
int main()
{
int size;int x=0;int work=1;int c;int v=0;
cout<<"Enter the size of the queue:";
cin>>size;
int queue[size];
while(work==1)
{
cout<<"Enter your choice\n";
cout<<"1.Insertion\n";
cout<<"2.Deletion\n";
cout<<"3.Display\n";
cout<<"4.EXIT\n";
cin>>c;
switch(c)
{
case 1:
cout<<"Enter the element:";
cin>>x;
enqueue(queue,size,x);
break;
case 2:
v=dequeue(queue,size);
if(v!=-1)
cout<<"Deleted element:"<<v<<"\n";
break;
case 3:
display(queue,size);
break;
case 4:
work=0;
cout<<"Exit\n";
break;
}
}
return 0;
}