-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
208 lines (158 loc) · 3.43 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
#include <iostream>
#include "stack.cpp"
using namespace std;
template <typename T>
class Queue
{
private:
Stack<T> stack1;
Stack<T> stack2;
public:
//Constructor with Default Parameters
Queue( int s = 10 ): stack1(s),stack2(s)
{
}
//Destructor
~Queue()
{
}
//To check whether the queue is empty
bool IsEmpty()
{
return (stack1.IsEmpty() && stack2.IsEmpty());
}
//To check whether the queue is full
bool IsFull()
{
return (stack1.IsFull());
}
//Copy Constructor
Queue(Queue& obj)
{
stack1 = obj.stack1;
stack2 = obj.stack2;
}
//Overloaded Assignment operator
Queue& operator = (Queue& rhs)
{
if(this != &rhs)
{
stack1 = rhs.stack1;
stack2 = rhs.stack2;
}
return *this;
}
/*Only Stack 1 is used to take the inputs. It will store all the inputs. Each element
is pushed into Stack 1. Its acting like an inbox.*/
bool Enqueue(T& data)
{
if(!IsFull())
{
stack1.Push(data);
return true;
}
return false;
}
/*If stack2 is empty then pop all elements from stack 1 and push them in
stack 2. Then pop the head the stack2. This will perform one dequeue operation.
Now if stack2 contains some elements so if the user calls for dequeue then simply
the head of stack2 will be popped. This will continue until the stack 2 becomes empty.
After it becomes empty then again all the elements from stack 1 are copied to stack 2.
And the same process is repeated.*/
bool Dequeue(T& data)
{
T temp;
if(!IsEmpty())
{
if(stack2.IsEmpty())
{
while(!stack1.IsEmpty())
{
stack1.Pop (temp);
stack2.Push(temp);
}
}
stack2.Pop(data);
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;
}