-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.cpp
83 lines (81 loc) · 1.95 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
#include <iostream>
#include <stdlib.h>
using namespace std;
#define N 100
class Queue
{
int front,rear,queue[N];
public:
Queue()
{
front=rear=-1;
}
void enQueue(int data)
{
if(rear==N-1)
{
cout<<"Queue if Full (Queue Overflow)\n";
return;
}
if(rear==-1)
front=rear=0;
else
rear++;
queue[rear] = data;
}
void deQueue()
{
if(front==-1)
{
cout<<"Queue is Empty (Queue Underflow)\n";
return;
}
cout<<"Deleted element from queue is: "<<queue[front]<<endl;
if(front==rear)
front=rear=-1;
else
front++;
}
void traversal()
{
if(front==-1)
{
cout<<"Queue is Empty (Queue Underflow)\n";
return;
}
cout<<"The elements present in the queue: \n";
for(int i=front;i<=rear;i++)
cout<<queue[i]<<" ";
cout<<endl;
}
};
int main()
{
Queue q;
int x,data;
do
{
cout<<"\nChoose the operation you want to perform: \n";
cout<<"1. Insertion to Queue \n";
cout<<"2. Deletion from Queue \n";
cout<<"3. Traversal of Queue \n";
cout<<"4. Quit \n";
cout<<"Enter your choice(1-4): ";
cin>>x;
switch(x)
{
case 1: cout<<"Enter the data to be stored: ";
cin>>data;
q.enQueue(data);
break;
case 2: q.deQueue();
break;
case 3: q.traversal();
break;
case 4: exit(0);
default: cout<<"Invalid Choice Entered\n";
}
}
while(true);
return 0;
}