-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathstack_with_arrays.cpp
More file actions
90 lines (84 loc) · 1.15 KB
/
stack_with_arrays.cpp
File metadata and controls
90 lines (84 loc) · 1.15 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
/*
*
* Title: Stack with array implementation.
* Author: Viswalahiri Swamy Hejeebu
* GitHub: https://github.com/Viswalahiri
* LinkedIn: https://in.linkedin.com/in/viswalahiri
*
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
# define SIZE 5
int arr[SIZE];
int top = -1;
void push(int value)
{
if(top==SIZE-1)
{
cout<<"Stack Overflow."<<endl;
}
else
{
top+=1;
arr[top]=value;
}
return;
}
void pop()
{
if(top==-1)
{
cout<<"Stack Underflow."<<endl;
}
else
{
cout<<"Deleted value is "<<arr[top]<<endl;
top-=1;
}
return;
}
void peek()
{
if(top==-1)
{
cout<<"Stack Empty."<<endl;
}
else
{
cout<<"Topmost element is "<<arr[top]<<endl;
}
return;
}
int main()
{
while(1)
{
cout<<"1 - push"<<endl;
cout<<"2 - pop"<<endl;
cout<<"3 - peek"<<endl;
cout<<"4 - exit"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Push what?"<<endl;
int to_push;
cin>>to_push;
push(to_push);
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
exit(0);
default:
cout<<"The option you have requested isn't present."<<endl;
}
}
return 0;
}