-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_operations2.cpp
193 lines (134 loc) · 3.11 KB
/
stack_operations2.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <iostream>
using namespace std;
class STACK{
private:
int top;
int stack[5];
public:
STACK(){
top = -1;
for(int i=0; i<5; i++){
stack[i] = 0;
}
}
bool isFull(){
if(top == 4){
return true;
}else{
return false;
}
}
bool isEmpty(){
if(top == - 1){
return true;
}else{
return false;
}
}
void push(int value){
if(isFull()){
cout<<"\n stack overflow \n ";
}else{
top++;
stack[top]=value;
}
}
int pop(){
if(isEmpty()){
cout<<"\n stack underflow \n ";
return 0;
}else{
int poptemp = stack[top];
stack[top]=0;
top--;
return poptemp;
}
}
void display(){
cout<<"\n all values in the stack are: \n";
for(int i=4; i>=0; i--){
cout<<stack[i]<<endl;
}
}
void change(int pos, int value){
stack[pos] = value;
cout<<"the "<<value<<" value changed at location "<<pos<<endl;
}
int peek(int pos){
if(isEmpty()){
cout<<"stack underflow \n";
return 0;
}else{
return stack[pos];
}
}
int count(){
return (top + 1);
}
};
int main(){
STACK STK;
int option, pos, value, countemp;
do{
cout<<"\n Enter your choice for operating on the stack: Enter 0 to exit \n";
cout<<"\n 1: Push() ";
cout<<" 2: Pop() ";
cout<<" 3: Print of stack() ";
cout<<" 4: isFull() ";
cout<<" 5: isEmpty() ";
cout<<" 6: change() ";
cout<<" 7: count() ";
cout<<" 8: peek() ";
cout<<" 9: clear screen \n";
cin>>option;
switch(option){
case 1:
cout<<"\n enter any value to push into the stack: ";
cin>>value;
STK.push(value);
break;
case 2:
cout<<"Popped value is: "<<STK.pop()<<endl;
break;
case 3:
STK.display();
break;
case 4:
if(STK.isFull()){
cout<<"\n stack is FULL \n";
}else{
cout<<"\n stack is NOT full \n";
}
break;
case 5:
if(STK.isEmpty()){
cout<<"\n Stack is EMPTY \n";
}else{
cout<<"\n Stack is NOT EMPTY \n";
}
break;
case 6:
cout<<"\n enter the postion where u want insertion: ";
cin>>pos;
cout<<"\n enter new value for the "<<pos<<" postion: ";
cin>>value;
STK.change(pos, value);
break;
case 7:
countemp = STK.count();
cout<<"\n Number of items in the stack are: "<<countemp<<endl;
break;
case 8:
cout<<"\n enter the postion where u want to access the element: ";
cin>>pos;
cout<<"value at position "<<pos<<" is "<<STK.peek(pos);
break;
case 9:
system("cls");
break;
default:
cout<<"\n enter proper option "<<endl;
}
}while(option != 0);
return 0;
}