-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.cpp
executable file
·172 lines (168 loc) · 3.62 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
172
#include <iostream>
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 == -10)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if (top == 4)
{
return true;
}
else
{
return false;
}
}
void push(int a)
{
if (isFull())
{
cout << "Stack Overflow" << endl;
}
else
{
top++;
arr[top] = a;
}
}
int pop()
{
if (isEmpty())
{
cout << "Stack Underflow" << endl;
return 0;
}
else
{
int popValue = arr[top];
arr[top] = 0;
top--;
return popValue;
}
}
int count()
{
return top + 1;
}
int peek(int x)
{
if (isEmpty())
{
cout << "Stack Empty" << endl;
return 0;
}
else
{
return arr[x];
}
}
void change(int pos, int value)
{
arr[pos] = value;
cout << "Value at position " << pos << "has changed";
}
void display()
{
cout << "All the Items in Stack are " << endl;
for (int i = 4; i >= 0; i--)
{
cout << arr[i] << endl;
}
}
};
int main()
{
Stack s1;
int option, position, value;
do
{
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;
cin >> option;
switch (option)
{
case 1:
cout << "Enter an Item" << endl;
cin >> value;
s1.push(value);
break;
case 2:
cout << "Pop Function Called - Poped Value:" << 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 The Index -->";
cin >> position;
cout << "Peek Value :" << s1.peek(position) << endl;
break;
case 6:
cout << "Number Of Items: " << s1.count() << endl;
break;
case 7:
cout << "Enter The Position: ";
cin >> position;
cout << "Enter The Value: ";
cin >> value;
s1.change(position, value);
break;
case 8:
cout << "Items Are :" << endl;
s1.display();
break;
case 9:
system("clear");
default:
cout << "Enter Proper Option" << endl;
}
} while (option != 0);
return 0;
}