-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks.cpp
More file actions
88 lines (80 loc) · 1.18 KB
/
stacks.cpp
File metadata and controls
88 lines (80 loc) · 1.18 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
#include<iostream>
using namespace std;
#define MAX 3
class stack {
private:
int top;
int *arr;
public:
stack() {
arr = new int[MAX];
top = -1;
}
bool isempty();
bool isfull();
void push(int &x);
int pop();
int peek();
};
bool stack::isempty() {
return(top<0);
}
bool stack::isfull() {
return(top>MAX-1);
}
void stack::push(int &x) {
if(isfull())
cout<<"Stack overflow\n";
else {
arr[++top]=x;
//cout<<arr[top]<<endl;
}
return;
}
int stack::pop() {
if(isempty())
{
cout<<"Stack underflow\n";
return -1;
}
else
cerr<<"top = "<<top<<" \t";
return(arr[top--]);
}
int stack::peek() {
if(isempty()) {
cout<<"The stack is empty\n";
return -1;
}
else
return (arr[top]);
}
int main() {
class stack st;
int x;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
for(int i= 0; i<5; i++) {
cin>>x;
st.push(x);
if(st.isfull()) {
cout<<"Stack overflow\n";
break;
}
}
for(int i = MAX-1; i>-5; i--) {
cout<<st.pop()<<endl;
if(st.isempty())
{
cout<<"Stack underflow\n";
break;
}
}
return 0;
}