File tree Expand file tree Collapse file tree 1 file changed +97
-0
lines changed Expand file tree Collapse file tree 1 file changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ class Stack
5
+ {
6
+ // Initailize objects of two in-built queues.
7
+ queue<int >q;
8
+ // To maintain current size of stack
9
+ int curr_size;
10
+
11
+ public:
12
+ Stack ()
13
+ {
14
+ curr_size = 0 ;
15
+ }
16
+
17
+ // Add an element to the Stack
18
+ void push (int x)
19
+ {
20
+ // Get previous size of queue
21
+ int s = q.size ();
22
+ // Push current element to queue
23
+ q.push (x);
24
+ // Dequeue all previous elements and Enqueue them after current element
25
+ for (int i=0 ; i<s; i++)
26
+ {
27
+ // This will add element at front into rear of the queue
28
+ q.push (q.front ());
29
+ // This will delete element at front of the queue
30
+ q.pop ();
31
+ }
32
+ curr_size++;
33
+ }
34
+
35
+ // Remove an element from the Stack
36
+ void pop (){
37
+ if (q.empty ())
38
+ {
39
+ cout << " Stack is Empty, thus deletion cannot be performed\n " ;
40
+ return ;
41
+ }
42
+ cout<<" The popped element is: " <<q.front ()<<endl;
43
+ q.pop ();
44
+ curr_size--;
45
+ return ;
46
+ }
47
+
48
+ void top ()
49
+ {
50
+ if (q.empty ())
51
+ {
52
+ cout<<" Stack is Empty\n " ;
53
+ return ;
54
+ }
55
+ cout<<" The Top Element of Stack is: " <<q.front ()<<endl;
56
+ return ;
57
+ }
58
+
59
+ int sizeStack ()
60
+ {
61
+ cout<<" Size of the Stack: " <<curr_size<<endl;
62
+ }
63
+ };
64
+
65
+ int main ()
66
+ {
67
+ Stack s;
68
+ int x, data;
69
+ do
70
+ {
71
+ cout<<" Main Menu \n " ;
72
+ cout<<" 1. Insertion to Stack (Push Operation) \n " ;
73
+ cout<<" 2. Deletion from Stack (Pop Operation) \n " ;
74
+ cout<<" 3. Return Top Element from Stack \n " ;
75
+ cout<<" 4. Size of Stack\n " ;
76
+ cout<<" 5. Quit \n " ;
77
+ cout<<" Enter your choice (1-5): " ;
78
+ cin>>x;
79
+ switch (x)
80
+ {
81
+ case 1 : cout<<" Enter the value to be inserted in Stack: " ;
82
+ cin>>data;
83
+ s.push (data);
84
+ break ;
85
+ case 2 : s.pop ();
86
+ break ;
87
+ case 3 : s.top ();
88
+ break ;
89
+ case 4 : s.sizeStack ();
90
+ break ;
91
+ case 5 : exit (0 );
92
+ default : cout<<" Invalid Choice Entered\n " ;
93
+ }
94
+ }
95
+ while (true );
96
+ return 0 ;
97
+ }
You can’t perform that action at this time.
0 commit comments