-
Notifications
You must be signed in to change notification settings - Fork 0
/
question_09.cpp
74 lines (59 loc) · 1.35 KB
/
question_09.cpp
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
/*
==> Implement stack using array <==
Question Link:- https://practice.geeksforgeeks.org/problems/implement-stack-using-array/1?page=1&difficulty[]=-1&category[]=Arrays&sortBy=submissions
Write a program to implement a Stack using Array. Your task is to use the class
as shown in the comments in the code editor and complete the functions push() and pop()
to implement a stack.
Example 1:
Input:
push(2)
push(3)
pop()
push(4)
pop()
Output: 3, 4
Explanation:
push(2) the stack will be {2}
push(3) the stack will be {2 3}
pop() poped element will be 3,
the stack will be {2}
push(4) the stack will be {2 4}
pop() poped element will be 4
Example 2:
Input:
pop()
push(4)
push(5)
pop()
Output: -1, 5
*/
#include <iostream>
using namespace std;
class MyStack {
private:
int arr[1000], top;
public:
MyStack() {
top = -1;
}
int pop();
void push(int n);
};
void MyStack :: push(int n) {
top++;
arr[top] = n;
}
int MyStack :: pop() {
if (top == -1) return -1;
int res = arr[top];
top--;
return res;
}
int main () {
MyStack s;
cout << s.pop() << endl;
s.push(4);
s.push(5);
cout << s.pop() << endl;
return 0;
}