-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.cpp
98 lines (90 loc) · 1.82 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
#include <iostream>
using namespace std;
const int size = 20;
int top = -1;
int stack[size];
int isFull()
{
if (top == size - 1)
return 1;
return 0;
}
int isEmpty()
{
if (top == -1)
return 1;
return 0;
}
void push(int data)
{
if (!isFull())
{
top++;
stack[top] = data;
}
else
cout << "Stack overflow" << endl;
}
void pop()
{
if (!isEmpty())
top--;
else
cout << "Stack underflow" << endl;
}
int peek()
{ //top()
if (!isEmpty())
return stack[top];
return INT_MIN;
}
int display()
{
cout << "++++++++++++++++++++++++++\n[";
for (int i = top; i >= 0; i--)
(i > 0) ? cout << stack[i] << ", " : cout << stack[i];
cout << "]\n++++++++++++++++++++++++++\n";
}
int main()
{
int choice = 6;
while (choice != 0)
{
cout << "## WHAT DO YOU WANT TO DO?" << endl
<< "0. Exit" << endl
<< "1. push" << endl
<< "2. pop" << endl
<< "3. peek/top" << endl
<< "4. display" << endl;
enteroption:
cout << "Type your answer: ";
cin >> choice;
if (choice < 0 || choice > 4)
{
cout << "Wrong choice. Choose again." << endl;
goto enteroption;
}
switch (choice)
{
case 1:
int numberToPush;
cout << "Enter a number to push: ";
cin >> numberToPush;
push(numberToPush);
break;
case 2:
pop();
break;
case 3:
cout << "++++++++++++++++++++++++++\n"
<< peek() << "\n++++++++++++++++++++++++++\n";
break;
case 4:
display();
break;
default:
cout << "Halted.";
break;
}
}
}