-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_operation.cpp
More file actions
101 lines (74 loc) · 1.34 KB
/
stack_operation.cpp
File metadata and controls
101 lines (74 loc) · 1.34 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include<bits/stdc++.h>
using namespace std;
class StackOperation
{
private:
/* data */
public:
int size;
int top;
int *S;
};
void display(class StackOperation st){
for (int i = st.top; i >=0; i--)
{
cout<<st.S[i]<<endl;
}
}
void push(class StackOperation *st, int x){
if (st->top==st->size-1)
{
cout<<"stack OverFlown";
}
else
{
st->top++;
st->S[st->top]=x;
}
}
int pop(class StackOperation *st)
{
int x= -1;
if (st->top ==-1)
{
cout<<"stack UnderFlown";
}
else
{
x=st->S[st->top];
st->top--;
}
return x;
}
int peek(class StackOperation st, int index)
{
int x=-1;
if (st.top-index+1<0){
cout<<"invalid positon";
}
else
{
x=st.S[st.top-index+1];
}
return x;
}
int main(){
class StackOperation st;
int size;
cout<<"enter size of stack";
cin>>size;
st.size=size;
st.top=-1;
st.S = new int[size];
push(&st, 10);
push(&st, 20);
push(&st, 30);
push(&st, 40);
display(st);
pop(&st);
pop(&st);
display(st);
peek(st,12);
return 0;
}
// all the possible condition has been checked and working fine. underflown overflown and invalid postion conditon also been checked