-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stack_using_array
94 lines (80 loc) · 1.77 KB
/
Stack_using_array
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
#include <iostream>
const int max_size = 100;
using namespace std;
class Stack{
private:
int arr[max_size];
int top = -1;
public:
void push(int value)
{
if (isFull()){
cout << "Stack Overflow.\n";
return;
}
arr[ top] = value;
}
void pop()
{
if (isEmpty()){
cout << "Stack Underflow.\n";
return;
}
--top;
}
void show()
{
if (top < 0){
cout << "Stack is Empty.";
return;
}
cout << "Stack elements : ";
for (int i;i<=top;i )
cout << arr[i] << " ";
cout << "\n";
}
bool isEmpty() const
{
return top < 0;
}
bool isFull() const
{
return top >= max_size-1;
}
};
int main()
{
Stack myStack;
cout << "1. For Push.\n2. For Pop.\n3. For Show.\n4. For isEmpty.\n5. For isFull.\n0. For Exit.";
while(true)
{
int choice;
cout << "\nEnter command : ";
cin >> choice;
switch (choice)
{
case 1:
int value;
cout << "Enter value to be Pushed : ";
cin >> value;
myStack.push(value);
break;
case 2:
myStack.pop();
break;
case 3:
myStack.show();
break;
case 4:
cout << "Is Empty ? : " << (myStack.isEmpty() ? "YES" : "NO");
break;
case 5:
cout << "Is Full ? : " << (myStack.isFull() ? "YES" : "NO");
break;
case 0:
return 0;
default:
cout << "Enter a valid command.";
}
}
}