forked from Shailendra-Java/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQueue.cpp
More file actions
109 lines (105 loc) · 2.52 KB
/
CircularQueue.cpp
File metadata and controls
109 lines (105 loc) · 2.52 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
#include<iostream>
#define MAX 5
using namespace std;
class CircularQueue
{
public:
int data[MAX], front, rear;
CircularQueue()
{
front = rear = -1;
}
int isEmpty()
{
if((front == -1 && rear == -1) || (front == rear))
return 1;
else
return 0;
}
int isFull()
{
if((front == 0 && rear == MAX-1)||(front == rear+1))
return 1;
else
return 0;
}
void push()
{
int num;
cout<<"Enter any number"<<endl;
cin>>num;
if(isFull())
{
cout<<"Queue overflow"<<endl;
return;
}
if(front == -1)
front = 0;
if(rear == MAX-1 && front != 0)
{
rear = 0;
}
else
rear++;
data[rear] = num;
cout<<num<<" inserted in queue"<<endl;
}
void pop()
{
int temp;
if(isEmpty())
{
cout<<"Queue is underflow"<<endl;
}
temp = data[front];
cout<<temp<<" deleted from queue"<<endl;
front++;
}
void display()
{
if(isEmpty())
{
cout<<"Queue is underflow"<<endl;
return;
}
if(rear > front)
{
for(int i=front; i<= rear; i++)
cout<<data[i]<<" ";
}
if(front>rear)
{
for(int k=0; k<= rear; k++)
cout<<data[k]<<" ";
for(int j= front; j< MAX; j++)
cout<<data[j]<<" ";
}
cout<<endl;
}
};
int main()
{
CircularQueue cq;
int opn;
char choice;
do
{
cout<<"1. Push\n2. Pop\n3. Display"<<endl;
cout<<"Enter your choice"<<endl;
cin>>opn;
switch(opn)
{
case 1:
cq.push();
break;
case 2:
cq.pop();
break;
case 3:
cq.display();
break;
}
cout<<"Do you want to continue(y/n)"<<endl;
cin>>choice;
}while(choice == 'y' || choice == 'Y');
}