-
Notifications
You must be signed in to change notification settings - Fork 0
/
Element_Stack.cpp
74 lines (59 loc) · 1.5 KB
/
Element_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
#include <iostream>
using namespace std;
class Stack {
private:
int arr[100];
int n;
public:
Stack() {
n=-1;
}
void push(int push_ele) {
if (n>=100-1) {
cout<<"Stack overflow. Cannot push element."<<endl;
} else {
arr[++n]=push_ele;
cout<<"Pushed element: "<<push_ele<<endl;
}
}
void pop(){
if (isEmpty()) {
cout<<"Stack is empty. Cannot pop element."<<endl;
} else {
int pop_ele=arr[n--];
cout<<"Popped element: "<<pop_ele<<endl;
}
}
bool isEmpty() {
return n==-1;
}
};
int main() {
Stack stack;
int c;
int ele;
cout<<"Stack Operations:"<<endl<<"1. Push"<<endl<<"2. Pop"<<endl<<"3. Exit"<<endl<<"Enter your choice: "<<endl;
cin>>c;
while (c!=3){
switch(c) {
case 1:
cout<<"Enter element to push: ";
cin>>ele;
stack.push(ele);
break;
case 2:
stack.pop();
break;
case 3:
break;
default:
cout<<"Invalid choice. Please try again."<<endl;
break;
}
cout<<endl;
cout<<"Stack Operations:"<<endl<<"1. Push"<<endl<<"2. Pop"<<endl<<"3. Exit"<<endl<<"Enter your choice: ";
cin>>c;
}
cout<<"Exiting..."<<endl;
return 0;
}