File tree 1 file changed +63
-0
lines changed
Implement stack using array - GFG
1 file changed +63
-0
lines changed Original file line number Diff line number Diff line change
1
+ // { Driver Code Starts
2
+ #include < bits/stdc++.h>
3
+ using namespace std ;
4
+
5
+ class MyStack
6
+ {
7
+ private:
8
+ int arr[1000 ];
9
+ int top;
10
+ public:
11
+ MyStack (){top=-1 ;}
12
+ int pop ();
13
+ void push (int );
14
+ };
15
+
16
+
17
+ int main ()
18
+ {
19
+
20
+ int T;
21
+ cin>>T;
22
+ while (T--)
23
+ {
24
+ MyStack *sq = new MyStack ();
25
+
26
+ int Q;
27
+ cin>>Q;
28
+ while (Q--){
29
+ int QueryType=0 ;
30
+ cin>>QueryType;
31
+ if (QueryType==1 )
32
+ {
33
+ int a;
34
+ cin>>a;
35
+ sq->push (a);
36
+ }else if (QueryType==2 ){
37
+ cout<<sq->pop ()<<" " ;
38
+
39
+ }
40
+ }
41
+ cout<<endl;
42
+ }
43
+ }
44
+
45
+ // } Driver Code Ends
46
+
47
+
48
+
49
+ // Function to push an integer into the stack.
50
+ void MyStack :: push(int x)
51
+ {
52
+ // Your Code
53
+ arr[++top] = x;
54
+ }
55
+
56
+ // Function to remove an item from top of the stack.
57
+ int MyStack :: pop()
58
+ {
59
+ // Your Code
60
+ if (top == -1 )
61
+ return -1 ;
62
+ return arr[top--];
63
+ }
You can’t perform that action at this time.
0 commit comments