-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularque.cpp
148 lines (145 loc) · 3.52 KB
/
circularque.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<iostream>
using namespace std;
class CircularQ
{
private:
int front;
int rear;
int arr[5];
int itemCount;
public:
CircularQ()
{
itemCount = 0;
rear = -1;
front = -1;
for(int i = 0;i < 5; i++)
{
arr[i] = 0;
}
}
bool isEmpty() //function to check queue whether it is empty or not
{
if(rear == -1 && front == -1)
return true;
else
return false;
}
bool isFull() //function to check queue whether it is full or not
{
if((rear + 1) % 5 == front)
return true;
else
return false;
}
void enqueue(int val) //function for enqueing element in the queue
{
if(isFull()) //if the queue is full
{
cout<<"\nQueue is full. Try dequeing some elements.";
return;
}
else if(isEmpty()) //if the queue is empty
{
front = 0;
rear = 0;
arr[rear] = val;
}
else
{
rear = (rear + 1) % 5;
arr[rear] = val;
}
itemCount++;
}
int dequeue() //function for dequeue
{
int x = 0;
if(isEmpty()) //if the queue is empty
{
cout<<"\nQueue is empty. Try enqueing something.";
return x;
}
else if(rear == front) //if the queue has only single element
{
x = arr[front];
front = -1;
rear = -1;
itemCount--;
return x;
}
else
{
x = arr[front];
arr[front] = 0;
front = (front + 1) % 5;
itemCount--;
return x;
}
}
int count()
{
return (itemCount);
}
void display()
{
for(int i = 0;i < 5; i++)
{
cout<<arr[i]<<"\t";
}
}
};
int main()
{
CircularQ cq;
int value, option;
do
{
cout<<"\n----------------QUEUE----------------"<<endl;
cout<<"1. isFull\n2. isEmpty\n3. Enqueue\n4. Dequeue\n5. Count\n6. Displaying Queue\n7. Exit\n8. Clear Screen"<<endl;
cout<<"Enter any choice you want: ";
cin>>option;
switch (option)
{
case 1:
if(cq.isFull())
cout<<"\nQueue is full."<<endl;
else
cout<<"There is some space.";
break;
case 2:
if(cq.isEmpty())
cout<<"Queue is empty."<<endl;
else
cout<<"Something is there in the Queue.";
break;
case 3:
cout<<"\nEnter something to enqueue: ";
cin>>value;
cq.enqueue(value);
cout<<value<<" has been inserted."<<endl;
break;
case 4:
cout<<"The number "<<cq.dequeue()<<" has been deleted."<<endl;
break;
case 5:
cout<<"Size of the Queue: "<<endl;
cout<<cq.count();
break;
case 6:
cout<<"Elements in the Queue displayed: ";
cq.display();
break;
case 7:
exit(0);
break;
case 8:
system("cls");
break;
default:
cout<<"Invalid Option"<<endl;
break;
}
} while (option != 0);
return 0;
}