-
Notifications
You must be signed in to change notification settings - Fork 33
/
stack.cpp
129 lines (114 loc) · 1.98 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
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
126
127
128
129
/*
Implementing stack using c++ class
*/
#include<iostream>
using namespace std;
class our_stack{
private:
int top;
int *elements;
int capacity;
public:
our_stack(int n){
top = -1;
capacity = n;
elements = new int[n];
}
~our_stack(){
delete elements;
}
void push(int x){
if(top+1<capacity){
elements[++top] = x;
}
else{
cout<<"Stack full"<<endl;
}
}
int pop(){
if(top>=0){
return elements[top--];
}
else{
return -1; // Stack empty
}
}
int top_element(){
if(top==-1){
return 999999999;
}
return elements[top];
}
bool isEmpty(){
if(top==-1){
return true;
}
return false;
}
bool isFull(){
if(top==99){
return true;
}
return false;
}
int size(){
return top+1;
}
};
int main(){
our_stack stack(3);
int choice,x;
A:
cout<<endl;
cout<<"Choose the operation to perform from following: "<<endl;
cout<<"1. Push"<<endl;
cout<<"2. Pop"<<endl;
cout<<"3. Top"<<endl;
cout<<"4. isEmpty"<<endl;
cout<<"5. isFull"<<endl;
cout<<"6. size"<<endl;
cout<<"7. exit the application"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice){
case 1: cout<<"Enter element to push: ";
cin>>x;
stack.push(x);
goto A;
break;
case 2: x = stack.pop();
if(x!=-1){
cout<<"Element popped from stack: "<<x<<endl;
}
else{
cout<<"Cannot pop. Stack empty"<<endl;
}
goto A;
break;
case 3: cout<<"Element at top: "<<stack.top_element()<<endl;
goto A;
break;
case 4: if(stack.isEmpty()){
cout<<"Stack empty"<<endl;
}
else{
cout<<"Stack not empty"<<endl;
}
goto A;
break;
case 5: if(stack.isFull()){
cout<<"Stack full"<<endl;
}
else{
cout<<"Stack not full"<<endl;
}
goto A;
break;
case 6: cout<<"Number of elements in stack: "<<stack.size()<<endl;
goto A;
break;
case 7: exit(0);
default: cout<<"Choose a valid option";
goto A;
}
}