Skip to content

Commit 1ae4f40

Browse files
authored
Merge pull request #36 from UTKARSHMOHAN2003/patch-2
Create implements a stack using array .cpp
2 parents d351ffa + 9013791 commit 1ae4f40

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
using namespace std;
3+
int stack[100], n=100, top=-1;
4+
void push(int val) {
5+
if(top>=n-1)
6+
cout<<"Stack Overflow"<<endl;
7+
else {
8+
top++;
9+
stack[top]=val;
10+
}
11+
}
12+
void pop() {
13+
if(top<=-1)
14+
cout<<"Stack Underflow"<<endl;
15+
else {
16+
cout<<"The popped element is "<< stack[top] <<endl;
17+
top--;
18+
}
19+
}
20+
void display() {
21+
if(top>=0) {
22+
cout<<"Stack elements are:";
23+
for(int i=top; i>=0; i--)
24+
cout<<stack[i]<<" ";
25+
cout<<endl;
26+
} else
27+
cout<<"Stack is empty";
28+
}
29+
int main() {
30+
int ch, val;
31+
cout<<"1) Push in stack"<<endl;
32+
cout<<"2) Pop from stack"<<endl;
33+
cout<<"3) Display stack"<<endl;
34+
cout<<"4) Exit"<<endl;
35+
do {
36+
cout<<"Enter choice: "<<endl;
37+
cin>>ch;
38+
switch(ch) {
39+
case 1: {
40+
cout<<"Enter value to be pushed:"<<endl;
41+
cin>>val;
42+
push(val);
43+
break;
44+
}
45+
case 2: {
46+
pop();
47+
break;
48+
}
49+
case 3: {
50+
display();
51+
break;
52+
}
53+
case 4: {
54+
cout<<"Exit"<<endl;
55+
break;
56+
}
57+
default: {
58+
cout<<"Invalid Choice"<<endl;
59+
}
60+
}
61+
}while(ch!=4);
62+
return 0;
63+
}

0 commit comments

Comments
 (0)