-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack in cpp
125 lines (110 loc) · 2.52 KB
/
stack in 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
using namespace std;
#define MAX 1000
class Stack {
int top;
public:
int a[MAX]; // Maximum size of Stack
Stack() { top = -1; }
bool push(int x);
int pop();
int peek();
bool isEmpty();
};
bool Stack::push(int x)
{
if (top >= (MAX - 1)) {
cout << "Stack Overflow";
return false;
}
else {
a[++top] = x;
cout << x << " pushed into stack\n";
return true;
}
}
int Stack::pop()
{
if (top < 0) {
cout << "Stack Underflow";
return 0;
}
else {
int x = a[top--];
return x;
}
}
int Stack::peek()
{
if (top < 0) {
cout << "Stack is Empty";
return 0;
}
else {
int x = a[top];
return x;
}
}
bool Stack::isEmpty()
{
return (top < 0);
}
int func(){
int x;
cout << "|__________________________________________________________|"<< endl;
cout << "|choice | description|" << endl;
cout <<"|_____ 1 _____|Insert or push the element in the array.|"<< endl;
cout <<"|_____ 2 _____|Pop the element in the array. |"<<endl;
cout << "|_____ 3 _____|Print the stack. |"<<endl;
cout << "|_____ 4 _____|View the top of the stack |"<<endl;
cout << "|_____ 5 _____|Check whether stack is empty or no |"<<endl;
cout << "|_____ 0 _____|Exit the program. |"<<endl;
cout << "|----------------------------------------------------------|"<< endl;
cout <<" CHOICE: ";
cin >> x;
return x;
}
int main()
{
class Stack s;
while (true)
{
int n;
n = func();
if (n==1)
{
int ele;
cout << "Enter the element you want to enter :";
cin >> ele;
s.push(ele);
continue;
}
if (n==2)
{
s.pop();
cout << s.pop() << " Popped from stack\n";
}
if (n==3)
{
cout<<"Elements present in stack : ";
while(!s.isEmpty())
{
// print top element in stack
cout<<s.peek()<<" ";
// remove top element in stack
cout << endl;
s.pop();
}
}
if (n==4){
cout << " Top of the stack is : "<< s.peek()<<endl;
}
if (n==5){
int kk =s.isEmpty();
if (kk==0)cout<<"No the stack is not empty."<<endl;
else{cout<<"Yes the stack is empty."<<endl;}
}
if (n==0){break;}
}
return 0;
}