-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
86 lines (75 loc) · 1.56 KB
/
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 MAX 1000
class Stack {
int top;
public:
int stk[MAX]; // Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
int topElement();
bool isEmpty();
void show();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
stk[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = stk[top--];
return x;
}
}
int Stack::topElement()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = stk[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
void Stack::show()
{
if(isEmpty()) { cout << "Stack empty\n"; return; }
cout << "The Elements of the stack are: \n";
for( int i=top; i>=0; i-- ) cout << stk[i] << endl;
}
// Driver program to test above functions
int main()
{
class Stack s;
s.push(10);
s.push(20);
s.push(30);
s.show();
cout << " Now the topElement value of stack is " << s.topElement() <<endl;
cout << s.pop() << " Popped from stack\n";
cout << " topElement value of stack is " << s.topElement() <<endl;
if (s.isEmpty())
cout << "Stack Is Empty\n";
else
cout << "Stack Is Not Empty\n";
return 0;
}