-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathqueueUsingStack.cpp
More file actions
112 lines (90 loc) · 1.71 KB
/
queueUsingStack.cpp
File metadata and controls
112 lines (90 loc) · 1.71 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
110
111
112
/*
Program : Queue using STL Stacks.
Author : © Vipin Kumar
Created on : March 16, 2018 20:48 IST
*/
#include <iostream>
#include <cstdlib>
#include <stack>
#include <conio.h>
#include <stdlib.h>
using namespace std;
class QueueUsingStack {
public:
int count;
stack <int> s1;
stack <int> s2;
QueueUsingStack ();
~QueueUsingStack ();
void enqueue (int);
int dequeue ();
};
QueueUsingStack ::QueueUsingStack () {
count = 0;
}
QueueUsingStack ::~QueueUsingStack () {
count = 0;
}
void QueueUsingStack::enqueue (int element) {
s1.push (element);
count++;
}
int QueueUsingStack::dequeue () {
int dequeuedElement;
if (s2.empty()) {
while (!s1.empty ()) {
s2.push (s1.top ());
s1.pop ();
}
}
if (!s2.empty()) {
dequeuedElement = s2.top ();
s2.pop ();
return dequeuedElement;
}
}
int main () {
QueueUsingStack q;
int choice;
int element;
int peekElement;
int currentSize;
stack <int> s1;
stack <int> s2;
system ("cls");
while (true) {
system ("cls");
cout << "\t\t***** QUEUE USING STACK\n\n";
cout << "1. ENQUEUE\n";
cout << "2. DEQUEUE\n";
cout << "3. FRONT ELEMENT\n";
cout << "4. SIZE\n";
cout << "5. DISPLAY\n";
cout << "6. EXIT\n";
cout << "Enter the choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter the element: ";
cin >> element;
q.enqueue (element);
getch ();
break;
case 2: if (!s2.empty()) {
peekElement = q.dequeue ();
cout << peekElement << "dequeued!";
getch ();
}
else {
cout << "Queue is empty!";
}
break;
case 6: exit (0);
break;
default:
cerr << "ERROR : Invalid Choice, enter again!";
break;
break;
}
}
return 0;
}