forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_implementation_using_array.cpp
69 lines (60 loc) · 1.11 KB
/
stack_implementation_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
59
60
61
62
63
64
65
66
67
68
69
#include <bits/stdc++.h>
using namespace std;
class stack1
{
private:
int size;
int top;
vector<int> s;
public:
stack1(int n){
size=n;
s.resize(n);
top=-1;
}
void display()
{
int i;
for (i = top; i >= 0; i--)
{
cout<<s[i]<<endl;
}
}
void push(int x)
{
if (top == size - 1)
{
cout<<"Stack Overflow"<<endl;
}
else
{
top++;
s[top] = x;
}
}
int pop()
{
int x = -1;
if (top == -1)
{
cout<<"Stack Underflow"<<endl;
}
else
{
x = s[top--];
}
return x;
}
};
int main()
{
stack1 st(4);
st.push(10);
st.push(20);
st.push(30);
st.push(40);
st.push(50);
st.push(60);
st.display();
return 0;
}