-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackArray.cpp
95 lines (91 loc) · 2.08 KB
/
stackArray.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
#include <bits/stdc++.h>
using namespace std;
int TOS = -1;
void mainMenu()
{
cout << endl;
cout << "1. Enter the element you want to add" << endl;
cout << "2. Enter the element you want to remove" << endl;
cout << "3. Peek TOS" << endl;
cout << "4. Exit" << endl;
}
void push(int stack[], int maxSize)
{
int userInput;
cout << "Enter the integer you want to add: ";
cin >> userInput;
cout << endl;
if (TOS + 1 >= maxSize)
{
cout << "Overflow" << endl;
return;
}
TOS++;
stack[TOS] = userInput;
cout << "Element " << userInput << " added to the stack." << endl;
return;
}
int pop(int stack[])
{
if (TOS < 0)
{
cout << "Underflow" << endl;
return -1;
}
TOS--;
cout << "TOS Element removed from the stack." << endl;
return stack[TOS + 1];
}
void traverseStack(int stack[])
{
cout << "This is not part of stack this only helps us to visualise the stack";
cout << endl;
for (int i = TOS; i >= 0; i--)
{
cout << "-------------" << endl;
cout << stack[i] << endl;
}
cout << "-------------" << endl;
}
int main()
{
int stackSize;
int elem;
cout << "Please enter the size of the stack: ";
cin >> stackSize;
int stack[stackSize];
int caseIn = 0;
while (caseIn != 4)
{
mainMenu();
cout << "Please enter the choice: ";
cin >> caseIn;
switch (caseIn)
{
case 1:
push(stack, stackSize);
traverseStack(stack);
break;
case 2:
elem = pop(stack);
traverseStack(stack);
break;
case 3:
if (TOS != -1)
{
traverseStack(stack);
cout << "TOS elememt is: " << stack[TOS] << endl;
}
else
cout << "No elememt in stack " << endl;
break;
case 4:
cout << "Exiting...";
caseIn = 4;
break;
default:
cout << "Invalid choice";
}
}
return 0;
}