-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack_using_array.cpp
58 lines (48 loc) · 1017 Bytes
/
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
55
56
57
58
// Time Complexity: O(N)
// Space Complexity: O(N)
#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)
{
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;
}