-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack-operation.cpp
189 lines (160 loc) · 3.34 KB
/
stack-operation.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
#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;
}
}
// check Stack empty or not
bool isEmpty()
{
if(top == -1)
return true;
else
return false;
}
// check Stack full or not
bool isFull()
{
if(top == 4)
return true;
else
return false;
}
// Push Operation
void push(int val)
{
if(isFull())
{
cout << "stack overflow" << endl;
}
else
{
top++;
arr[top] = val;
}
}
// Pop operation
int pop()
{
if(isEmpty())
{
cout << "stack underflow" << endl;
return 0;
}
else
{
int popValue = arr[top];
arr[top] = 0;
top--;
return popValue;
}
}
// Count operation
int count()
{
return(top+1);
}
// Peek operation
int peek(int pos)
{
if(isEmpty())
{
cout << "Stack Underflow" << endl;
return 0;
}
else
{
return arr[pos];
}
}
// Change operation
void change(int pos, int val)
{
arr[pos] = val;
cout << "Value changed at location " << pos << endl ;
for(int i = 4; i >= 0; i++)
{
cout << arr[i] << endl;
}
}
// Display operation
void display()
{
cout << "All the value in the stack are : ";
for(int i = 4; i<=0; i++)
{
cout << arr[i] << endl ;
}
}
};
// Main Function.
int main()
{
stack s1;
int option, position, value;
do
{
cout << "\n\nWhat operation you want to perform ?/n Enter 0 to exit \n";
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;
cout << "Enter option : " ;
cin >> option ;
switch(option)
{
case 1 : cout << "\nEnter element to be pushed : ";
cin >> value;
s1.push(value);
break;
case 2 : cout << "\nPop done" << s1.pop() << endl ;
break;
case 3 : if(s1.isEmpty())
cout << "\nStack Empty" << endl;
else
cout << "\nStack is not empty" << endl;
break;
case 4 : if(s1.isFull())
cout << "\nStack is full " << endl;
else
cout << "\nStack not full " << endl;
break;
case 5 : cout << "\nEnter the position of the item you want to peek : ";
cin >> position;
cout << "\npeek position called at positon " << position << "is"<< endl;
s1.peek(position);
break;
case 6 : cout << "\nCount function called - Number of items in stack are : " << s1.count() << endl;
break;
case 7 : cout << "\nChanged function called " << endl ;
cout << "\nEnter the position you want to change : ";
cin >> position;
cout << "\nEnter the value of item : ";
cin >> value;
s1.change(position,value);
break;
case 8 : cout << "\nDisplay function called - " << endl;
s1.display();
break;
case 9 : system("cls");
break;
default : cout << "Enter approcriate option ";
}
}while(option != 0);
}