-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.cpp
87 lines (80 loc) · 1.52 KB
/
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
#include <iostream>
#define SIZE 10
using namespace std;
class Queue
{ int front=-1;
int rear=-1;
public:
void enqueue(int queue[],int);
void dequeue(int queue[],int);
void display(int queue[]);
};
int main()
{ Queue obj;
while(true)
{
int choice,queue[SIZE];
cout<<"Press 1 to insert the element"<<endl;
cout<<"Press 2 to delete the element"<<endl;
cout<<"Press 3 to display"<<endl;
cout<<"Press 4 to exit"<<endl;
cout<<"Enter the choice from the above menu:";
cin>>choice;
switch (choice)
{
case 1: obj.enqueue(queue,SIZE);
break;
case 2: obj.dequeue(queue,SIZE);
break;
case 3: obj.display(queue);
break;
case 4: exit(0);
default: "Sorry!! you have entered wrong choice";
}
}
return 0;
}
void Queue::enqueue(int queue[],int n)
{
int value;
cout<<"Enter the value to push:";
cin>>value;
if(rear==-1)
{ rear++;
front++;
queue[rear]=value;
cout<<endl<<value<<endl<<"has been inserted in the queue"<<endl;
}
else
{
if(rear==n-1)
{
cout<<"Queue Overflow";
}
else
{
rear++;
queue[rear]=value;
cout<<endl<<value<<endl<<"has been inserted in the queue"<<endl;
}
}
}
void Queue::dequeue(int queue[],int n)
{
if((front==-1)||(front>rear))
{
cout<<"Queue underflow"<<endl;
}
else
{
cout<<"The deleted element from the queue is:"<<queue[front]<<endl;
front=front+1;
}
}
void Queue::display(int queue[])
{
for(int j=front;j<=rear;j++)
{
cout<<queue[j]<<endl;
}
}