-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_implentation(arrays).cpp
More file actions
73 lines (73 loc) · 1.54 KB
/
Stack_implentation(arrays).cpp
File metadata and controls
73 lines (73 loc) · 1.54 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
#include<iostream>
using namespace std;
void Push(int Stack[] ,int& top ,int x,int n){
if(top>=n-1){
cout<<"Stack overflow\n";
return;
}
top++;
Stack[top]=x;
}
void Display(int Stack[],int top){
if(top==-1){
cout<<"Stack is empty!\n";
return;
}
int i =top;
while (i>=0)
{
cout<<Stack[i]<<" ";
i--;
}
cout<<"\n";
}
void Pop(int Stack[] ,int& top){
if(top==-1){
cout<<"Stack underflow on Pop!\n";
return;
}
int x = Stack[top];
top--;
cout<<"Element popped is : "<<x<<"\n";
}
void Peek(int Stack[],int top){
if(top==-1){
cout<<"Stack is empty!\n";
return;
}
cout<<"Stack top is : "<<Stack[top]<<endl;
}
int main()
{
int Stack[10];
int n, choice , x,e=0,top=-1;
cout<<"enter the size of the stack: ";
cin>>n;
do
{
cout<<"enter your choice\n"
<<"1.Push\n2.Display\n3.Pop\n4.Peek\n";
cin>>choice;
switch (choice)
{
case 1:
cout<<"enter the element you want to push: ";
cin>>x;
Push(Stack , top ,x ,n);
break;
case 2:
cout<<"the elements are: \n";
Display(Stack,top);
break;
case 3:
Pop(Stack , top);
break;
case 4:
Peek(Stack ,top);
break;
default:
break;
}
} while (choice<=4);
return 0;
}