forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic_Stack.cpp
86 lines (65 loc) · 1.31 KB
/
Dynamic_Stack.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
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
using namespace std;
#define BOUND 4
int top = -1;
int length = 0;
// function to create new stack
int* create_new(int* a)
{
int* new_a = new int[length + BOUND];
for (int i = 0; i < length; i++)
new_a[i] = a[i];
length += BOUND;
return new_a;
}
// function to push new element
int* push(int* a, int element)
{
// if stack is full, create new one
if (top == length - 1)
a = create_new(a);
a[++top] = element;
return a;
}
// function to pop an element
void pop(int* a)
{
top--;
}
void display(int* a)
{
// if top is -1, that means stack is empty
if (top == -1)
cout << "Stack is Empty" << endl;
else {
cout << "Stack: ";
for (int i = 0; i <= top; i++)
cout << a[i] << " ";
cout << endl;
}
}
int main()
{
int *a = create_new(a);
a = push(a, 1);
a = push(a, 2);
a = push(a, 3);
a = push(a, 4);
display(a);
a = push(a, 5);
a = push(a, 6);
display(a);
a = push(a, 7);
a = push(a, 8);
display(a);
a = push(a, 9);
display(a);
return 0;
}
/*
Output :
Stack: 1 2 3 4
Stack: 1 2 3 4 5 6
Stack: 1 2 3 4 5 6 7 8
Stack: 1 2 3 4 5 6 7 8 9
*/