forked from sanketpatil02/Code-Overflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
All Stack Operations
169 lines (137 loc) · 2.59 KB
/
All Stack Operations
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
#include<iostream>
#include<string>
using namespace std;
class stack{
private:
int top;
int arr[5];
public:
stack(){
top=-1;
for(int i=0;i<5;i++){
arr[i]=0;
}
}
bool isEmpty(){
if(top==-1)
return true;
else
return false;
}
bool isFull(){
if (top==4)
return true;
else
return false;
}
void push(int val){
if(isFull()){
cout<<"stack full";
}
else{
top++;
arr[top] =val;
}
}
int pop(){
if(isEmpty()){
cout<<"stack is empty";
return 0;
}
else{
int popvalue=arr[top];
arr[top]=0;
top--;
return popvalue;
}
}
int coun(){
return (top+1);
}
int peek(int pos){
if(isEmpty()){
cout<<"stack empty<<endl";
return 0;
}
else{
return arr[pos];
}
}
void change(int pos,int val){
arr[pos] = val;
cout<<"value changed at location"<<pos<<endl;
}
void display(){
cout<<"all the values are"<<endl;
for(int i=4;i>=0;i--){
cout<<arr[i]<<endl;
}
}
};
int main(){
stack s1;
int option,position,value;
cout<<"MENU"<<endl;
do{
cout<<"select option, enter 0 to exit"<<endl;
cout<<"1. push"<<endl;
cout<<"2. pop"<<endl;
cout<<"3. isEmpty"<<endl;
cout<<"4. isFull"<<endl;
cout<<"5. peek"<<endl;
cout<<"6. count"<<endl;
cout<<"7. change"<<endl;
cout<<"8. Display"<<endl;
cout<<"9. clear screen"<<endl<<endl;
cin>>option;
switch(option){
case 0:
break;
case 1:
cout<<"enter value to push"<<endl;
cin>>value;
s1.push(value);
break;
case 2:
cout<<"pop function called..."<<s1.pop()<<endl;
break;
case 3:
if(s1.isEmpty())
cout<<"stack is empty"<<endl;
else
cout<<"stack is not empty"<<endl;
break;
case 4:
if(s1.isFull())
cout<<"stack is Full"<<endl;
else
cout<<"stack is not Full"<<endl;
break;
case 5:
cout<<"enter position of iteam you want to peak"<<endl;
cin>>position;
cout<<"peek function called...value at position"<<position<<"is"<<s1.peek(position)<<endl;
break;
case 6:
cout<<"number of items in stack are"<<s1.coun()<<endl;
break;
case 7:
cout<<"enter position of item you want to change:";
cin>>position;
cout<<endl<<"enter value of item you want to change:";
cin>>value;
s1.change(position,value);
break;
case 8:
cout<<"display function called"<<endl;
s1.display();
break;
case 9:
system("cls");
break;
default:
cout<<"enter proper option:"<<endl;
}
}while(option !=0);
return 0;
}