-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_operations.cpp
150 lines (95 loc) · 2.55 KB
/
Stack_operations.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
#include <iostream>
using namespace std;
//... this piece of code demonstrates some operations of stacks:
//... for instance: push(putting an element into the stack, popping(taking out an element form stack
//... printstack(displaying the elemtns of the stack, & checking the emptyness of the stack...
void push(int stack[], int &top, int value, int size){
if(top == size-1 ){
cout<<"\n Stack is overflow \n ";
}
else if(top != size - 1){
top++;
stack[top] = value;
}
}
int pop(int stack[], int &top){
int temp;
if(top == -1){
cout<<"\n Stack is underflow \n";
}
else if(top != - 1){
temp = stack[top];
top--;
}
return temp;
}
void printstack(int stack[], int &top){
if(top != -1){
cout<<"\n The stack elements are: \n";
for(int i=0; i<=top; i++){
cout<<stack[i]<<" ";
}
cout<<"\n";
}
}
string isEmpty(int &top){
string check;
if(top == -1){
check = "EMPTY \n";
}else{
check = " is NOT empty \n";
}
return check;
}
string isFull(int &top, int size){
string check;
if(top == size-1){
check = "FULL \n";
}else{
check = " is NOT full \n";
}
return check;
}
void change(int stack[], int value, int pos){
stack[pos] = value;
cout<<"\n value changed at location \n"<<pos<<endl;
}
int main(){
int size;
cout<<"Enter the size for the stack: ";
cin>>size;
int top = -1;
int stack[size];
int choice, value;
do{
cout<<"\n Enter your choice for operating on the stack: \n";
cout<<"1: Push 2: Pop 3: Print of stack 4: isEmpty 5: isFull 6: change ";
cin>>choice;
if(choice == 1){
cout<<"\n Enter any integer value for pushing into the stack: ";
cin>>value;
push(stack, top, value, size);
}else if(choice == 2){
int popping = pop(stack, top);
cout<<"\n Popped value is: "<<popping<<endl;
}else if(choice == 3){
printstack(stack, top);
}else if(choice == 4){
string isemp = isEmpty(top);
cout<<"\n stack "<<isemp<<endl;
}else if(choice == 5){
string isful = isFull(top, size);
cout<<"\n stack "<<isful<<endl;
}else if(choice ==6){
int pos, newvalue;
cout<<"\n enter the position where your want the change: ";
cin>>pos;
cout<<"\n enter the value you want to change at "<<pos<<" positon: ";
cin>>newvalue;
change(stack, newvalue, pos);
}else if(choice == 7){
exit(0);
}
}while(true);
return 0;
}