File tree Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Expand file tree Collapse file tree 1 file changed +66
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Stack {
2
+ public:
3
+ queue<int > main;
4
+ queue<int > temp;
5
+ // Push element x onto stack.
6
+ void push (int x) {
7
+ main.push (x);
8
+ }
9
+
10
+ // Removes the element on top of the stack.
11
+ void pop () {
12
+ if (!main.empty ()){
13
+ while (main.size ()>1 ){
14
+ int x=main.front ();
15
+ main.pop ();
16
+ temp.push (x);
17
+ }
18
+ main.pop ();
19
+ }
20
+ else {
21
+ int s=temp.size ();
22
+ int i=0 ;
23
+ while (i<s-1 ){
24
+ int x=temp.front ();
25
+ temp.pop ();
26
+ temp.push (x);
27
+ i++;
28
+ }
29
+ temp.pop ();
30
+ }
31
+ }
32
+
33
+ // Get the top element.
34
+ int top () {
35
+ if (!main.empty ()){
36
+ while (main.size ()>1 ){
37
+ int x=main.front ();
38
+ main.pop ();
39
+ temp.push (x);
40
+ }
41
+ return main.front ();
42
+ }
43
+ else {
44
+ int s=temp.size ();
45
+ int i=0 ;
46
+ while (i<s-1 ){
47
+ int x=temp.front ();
48
+ temp.pop ();
49
+ temp.push (x);
50
+ i++;
51
+ }
52
+ int y=temp.front ();
53
+ temp.pop ();
54
+ temp.push (y);
55
+ return y;
56
+ }
57
+ }
58
+
59
+ // Return whether the stack is empty.
60
+ bool empty () {
61
+ if (main.empty () && temp.empty ()) return true ;
62
+ else return false ;
63
+
64
+ }
65
+
66
+ };
You can’t perform that action at this time.
0 commit comments