-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticStack.cpp
More file actions
77 lines (74 loc) · 1.72 KB
/
Copy pathStaticStack.cpp
File metadata and controls
77 lines (74 loc) · 1.72 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
//Static Implementation using Array
#include <iostream>
using namespace std;
const int Size=10;
struct Stack {
int arr[Size];
int top = -1;
};
// Push Function
void push(Stack &stack, int value) {
if(stack.top == Size-1) {
cout << "Stack Overflow! You cannot push more Elements " << value << endl;
return;
}
stack.arr[++stack.top]=value;
cout<<value<<" pushed to stack."<<endl;
}
// Pop Functon
void pop(Stack &stack) {
if(stack.top == -1) {
cout<<"Stack Underflow! You cannot pop more elements."<<endl;
return;
}
cout<<stack.arr[stack.top--]<<"element is popped from stack."<<endl;
}
//Display Stack
void display(Stack &stack) {
if(stack.top == -1) {
cout << "Stack is empty."<<endl;
return;
}
cout << "Stack elements From top to bottom:";
for (int i = stack.top;i >= 0; i--) {
cout<< stack.arr[i]<<" ";
}
cout<<endl;
}
// Main function to test stack
int main() {
Stack stack;
int choice,value;
while(true){
cout<<"1: Push"<<endl;
cout<<"2: Pop"<<endl;
cout<<"3: View Elements"<<endl;
cout<<"4: Exit"<<endl;
cout<<"Enter which operation would you like to perform : "<<endl;
cin>>choice;
switch (choice)
{
case 1:
cout<<"Enter value to be pushed:";
cin>>value;
push(stack, value);
break;
case 2:
pop(stack);
break;
case 3:
display(stack);
break;
case 4:
cout<<"Exiting...\n";
break;
default:
break;
}
if (choice >= 4)
{
break;
}
}
return 0;
}