-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStack.cpp
171 lines (155 loc) · 3.28 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
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
#include <iostream>
#include <string>
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())
{
std::cout << "Stack Overflow!" << std::endl;
}
else
{
top++;
arr[top] = val;
}
}
int pop()
{
if (isEmpty())
{
std::cout << "Stack Underflow!" << std::endl;
return 0;
}
else
{
int popValue = arr[top];
arr[top] = 0;
top--;
return popValue;
}
}
int count()
{
return(top + 1);
}
int peek(int pos)
{
if(isEmpty())
{
std::cout << "Stack Underflow!" << std::endl;
return 0;
}
else
{
return arr[pos];
}
}
void change(int pos, int val)
{
arr[pos] = val;
std::cout << "Value changed at location" << pos << std::endl;
}
void display()
{
std::cout << "All values in the Stack are " << std::endl;
for (int i = 4; i >= 0; i--)
{
std::cout << arr[i] << std::endl;
}
}
};
int main()
{
Stack s1;
int option, position, value;
do
{
std::cout << "What operation do you want to perform? Select Option number. Enter 0 to exit" << std::endl;
std::cout << "1. Push()" << std::endl;
std::cout << "2. Pop()" << std::endl;
std::cout << "3. isEmpty()_" << std::endl;
std::cout << "4. isFull()" << std::endl;
std::cout << "5. peek()" << std::endl;
std::cout << "6. count()" << std::endl;
std::cout << "7. change()" << std::endl;
std::cout << "8. display()" << std::endl;
std::cout << "9. Clear Screen" << std::endl;
std::cin >> option;
switch (option)
{
case 1:
std::cout << "Enter an item to push in the stack" << std::endl;
std::cin >> value;
s1.push(value);
break;
case 2:
std::cout << "Pop function called - Popped Value: " << s1.pop() << std::endl;
break;
case 3:
if (s1.isEmpty())
std::cout << "Stack is empty" << std::endl;
else
std::cout << "Stack is not empty" << std::endl;
break;
case 4:
if(s1.isFull())
std::cout << "Stack is full" << std::endl;
else
std::cout << "Stack is not full" << std::endl;
break;
case 5:
std::cout << "Enter position of item you want to peek: " << std::endl;
std::cin >> position;
std::cout << "Peek function called - Value at position " << position << "is " << s1.peek(position) << std::endl;
break;
case 6:
std::cout << "Count function called - Number of items in the stack are: " << s1.count() << std::endl;
break;
case 7:
std::cout << "Change function called - " << std::endl;
std::cout << "Enter position of item you want to change : ";
std::cin >> position;
std::cout << std::endl;
std::cout << "Enter value of item you want to change : ";
std::cin >> value;
s1.change(position, value);
break;
case 8:
std::cout << "Display function called" << std::endl;
s1.display();
break;
case 9:
system("cls");
break;
default:
std::cout << "Enter proper option number " << std::endl;
}
} while (option !=0);
return 0;
}