File tree Expand file tree Collapse file tree 1 file changed +15
-19
lines changed Expand file tree Collapse file tree 1 file changed +15
-19
lines changed Original file line number Diff line number Diff line change @@ -6,37 +6,33 @@ struct Queue {
6
6
// Add an item to the queue
7
7
void enQueue (int x)
8
8
{
9
- // Move all elements from first stack (s1) to second stack (s2)
10
- while (!s1.empty ()) {
11
- s2.push (s1.top ());
12
- s1.pop ();
13
- }
14
- // Push item into first stack (s1)
9
+ // Push item into the first stack (s1)
15
10
s1.push (x);
16
- // Push everything back to first stack (s1)
17
- while (!s2.empty ()) {
18
- s1.push (s2.top ());
19
- s2.pop ();
20
- }
21
11
}
22
-
23
12
// Remove an item from the queue
24
13
int deQueue ()
25
14
{
26
- // If first stack (s1) is empty, then exit
27
- if (s1.empty ()) {
15
+ // If both stacks are empty, then exit
16
+ if (s1.empty () && s2. empty ()) {
28
17
cout << " Queue is Empty, thus deletion cannot be performed\n " ;
29
18
return -1 ;
30
19
}
31
20
32
- // Else return top of first stack (s1)
33
- int x = s1.top ();
34
- s1.pop ();
21
+ // If second stack (s2) is empty, move elements from first stack (s1) to second stack (s2)
22
+ if (s2.empty ()) {
23
+ while (!s1.empty ()){
24
+ s2.push (s1.top ());
25
+ s1.pop ();
26
+ }
27
+ }
28
+ // Return the top item from second stack (s2)
29
+ int x = s2.top ();
30
+ s2.pop ();
35
31
return x;
36
32
}
37
33
void sizeQueue ()
38
34
{
39
- cout<<" Size of the Queue: " <<s1.size ()<<endl;
35
+ cout<<" Size of the Queue: " <<s1.size ()+s2. size () <<endl;
40
36
}
41
37
};
42
38
@@ -45,7 +41,7 @@ int main()
45
41
Queue q;
46
42
int x, data;
47
43
do
48
- {
44
+ {
49
45
cout<<" \n Main Menu: \n " ;
50
46
cout<<" 1. Insertion to Queue \n " ;
51
47
cout<<" 2. Deletion from Queue \n " ;
You can’t perform that action at this time.
0 commit comments