-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
90 lines (70 loc) · 1.62 KB
/
Stack.cpp
File metadata and controls
90 lines (70 loc) · 1.62 KB
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
#include <iostream>
#include <cstddef>
#include <climits>
#include "Stack.h"
bool Stack::isEmpty() const
{
if(top == -1)
return true;
else
return false;
}
bool Stack::push(const int &el) const
{
if(top >= cap - 1)
return false;
else
{
arr[++top] = el;
return true;
}
}
int Stack::pop() const
{
return arr[--top];
}
int Stack::peek() const
{
return arr[top];
}
~Stack()
{
delete [] arr;
}
int main()
{
int size = 0, el = 0, ch = 0;
std::cout << "Enter the desired stack size" << std::endl;
std::cin >> size;
Stack a(size);
do{
std::cout << " ~MENU~ " << std::endl;
std::cout << "1->PUSH\n" << "2->POP\n" << "3->PEEK\n" << "4->EXIT" << std::endl;
std::cout << "Please enter your choice" << std::endl;
std::cin >> ch;
switch(ch)
{
case 1 : std::cout << "Enter the element you want to push" << std::endl;
std::cin >> el;
if(a.push(el))
std::cout << "Pushed Successfully!" << std::endl;
else
std::cout << "Stack overflow" << std::endl;
break;
case 2 : if(!a.isEmpty())
std::cout << a.pop() << std::endl;
else
std::cout << "Empty stack! Operation aborted." << std::endl;
break;
case 3 : if(a.isEmpty())
std::cout << "Empty stack! Operation aborted." << std::endl;
else
std::cout << a.peek() << std::endl;
case 4 :
break;
default : std::cout << "Invalid choice" << std::endl;
}
}
while(ch != 4);
std::cout << "THANK YOU" << std::endl;
}