-
Notifications
You must be signed in to change notification settings - Fork 77
/
Implement_Stack_using_Array.cpp
54 lines (46 loc) · 1.1 KB
/
Implement_Stack_using_Array.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
// Problem statement: Implement a stack using an array.
// Explanation:
// push(): Insert the element in the stack.
// pop(): Remove and return the topmost element of the stack.
// top(): Return the topmost element of the stack
// size(): Return the number of remaining elements in the stack.
#include<bits/stdc++.h>
using namespace std;
class Stack {
int size;
int * arr;
int top;
public:
Stack() {
top = -1;
size = 1000;
arr = new int[size];
}
void push(int x) {
top++;
arr[top] = x;
}
int pop() {
int x = arr[top];
top--;
return x;
}
int Top() {
return arr[top];
}
int Size() {
return top + 1;
}
};
int main() {
Stack s;
s.push(6);
s.push(3);
s.push(7);
cout << "Top of stack is before deleting any element " << s.Top() << endl;
cout << "Size of stack before deleting any element " << s.Size() << endl;
cout << "The element deleted is " << s.pop() << endl;
cout << "Size of stack after deleting an element " << s.Size() << endl;
cout << "Top of stack after deleting an element " << s.Top() << endl;
return 0;
}