-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Queue.cpp
233 lines (179 loc) · 3.38 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <cstdlib>
using namespace std;
template <typename T>
class Queue
{
private:
T* arr;
int front;
int rear;
int maxSize;
int currSize;
public:
//Constructor with Default Parameters
Queue (int s = 10)
{
if(s<=0)
s = 10;
arr = new T [s];
maxSize = s;
currSize = 0;
front = 0;
rear = -1;
}
//Destructor
~Queue ()
{
if(arr)
delete arr;
}
//To check whether the queue is empty
bool IsEmpty()
{
return (currSize == 0);
}
//To check whether the queue is full
bool IsFull()
{
return (currSize == maxSize);
}
//Copy Constructor
Queue (Queue& obj)
{
arr = new T [obj.maxSize];
for (int i = 0; i < obj.maxSize; ++i)
{
arr[i] = obj.arr[i];
}
currSize = obj.currSize;
maxSize = obj.maxSize;
front = obj.front;
rear = obj.rear;
}
//Overloaded Assignment Operator
Queue& operator = (Queue& rhs)
{
if(this != &rhs)
{
if(arr)
delete arr;
arr = new T [rhs.maxSize];
for (int i = 0; i < rhs.maxSize; ++i)
{
arr[i] = rhs.arr[i];
}
currSize = rhs.currSize;
maxSize = rhs.maxSize;
front = rhs.front;
rear = rhs.rear;
}
return *this;
}
//To add an element at the rear of the queue
bool Enqueue( T& data)
{
if(!IsFull())
{
if(rear == maxSize - 1)//To aid wraparound
rear = -1;
arr[++rear] = data;
currSize++;
return true;
}
return false;
}
//To remove an element from the front of the Queue
bool Dequeue ( T& data )
{
if(!IsEmpty())
{
if(front == maxSize)//To aid wraparound
{
front = 0;
}
data = arr[front++];
currSize--;
return true;
}
return false;
}
void displayFunctions()
{
cout << "\n\nThe class Queue has the following functions : ";
cout << "\n1)Enqueue \n2)Dequeue \n3)IsEmpty \n4)IsFull ";
cout << "\nEnter the option number. To exit, enter -1.\nOption Number : ";
}
void displayQueue()
{
T temp;
cout << "\n\nThe queue is : ";
while( !IsEmpty())
{
Dequeue(temp);
cout << endl << temp;
}
}
};
//-------------Class Ended--------------------------------
void driver()
{
int size;
cout << "Enter the maxsize of the queue : ";
cin >> size;
Queue<int> que1(size);
int userInput = 0;
int data;
while (userInput != -1)
{
que1.displayFunctions();
cin >> userInput;
if (userInput == 1)
{
cout << "\nEnter the data to insert into the queue : ";
cin >> data;
if ( que1.Enqueue(data) )
cout << "\nEnqueue Successful.";
else
cout << "\nEnqueue Unsuccessful.";
}
else if (userInput == 2)
{
if( que1.Dequeue(data) )
cout << "\nThe Dequeued data item is : " << data;
else
cout << "\nDequeue Unsuccessful.";
}
else if (userInput == 3)
{
if (que1.IsEmpty())
cout << "\nThe queue is empty.";
else
cout << "\nThe queue isn't empty.";
}
else if (userInput == 4)
{
if (que1.IsFull())
cout << "\nThe queue is full.";
else
cout << "\nThe queue isn't full.";
}
}
cout << "\nTesting the copy constructor :";
Queue<int> que2 = que1;
cout << "\nqueue 2 is a copy of queue 1 constructed through Copy Constructor.";
if(! que1.IsEmpty() )
que1.displayQueue();
else
cout << "\nqueue 1 is empty.";
if(! que2.IsEmpty() )
que2.displayQueue();
else
cout << "\nqueue 2 is empty.";
cout << "\n\nSimilarly the overloaded assignment operator can be tested. " << endl;
}
int main ()
{
driver();
return 0;
}