-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.cpp
More file actions
73 lines (61 loc) · 1.62 KB
/
Copy pathStack.cpp
File metadata and controls
73 lines (61 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
#include<iostream>
using namespace std;
int main()
{
int length;
cout<<"Enter the length of the stack"<<endl;
cin>>length;
int s[length];
int top;
char c;
int item;
cout<<"Do you want to insert elements in your stack?..If yes,type y..else type n"<<endl;
cin>>c;
if(c=='y')
{
cout<<"Enter how many elements you want in stack"<<endl;
cin>>top;
cout<<"Enter elements"<<endl;
for(int i=0;i<top;i++)
{
cin>>s[i];
}
cout<<"Stack before any push or pop"<<endl;
for(int i=0;i<top;i++)
{
cout<<s[i]<<" ";
}
cout<<endl<<endl;
cout<<"Now Push an item into stack"<<endl<<endl;
if(top==length)
{
cout<<"Overflow!!!..Can't push any item"<<endl<<endl;
}else{
cin>>item;
s[top]=item;
cout<<"Stack after pushing an item"<<endl;
for(int i=0;i<=top;i++)
{
cout<<s[i]<<" ";
}
top=top+1;
}
cout<<endl<<endl;
cout<<"Now pop an item from the stack"<<endl;
top=top-1;
cout<<"Stack after popping an item"<<endl;
for(int i=0;i<top;i++)
{
cout<<s[i]<<" ";
}
}else if(c=='n')
{
cout<<"Underflow!! Need to insert item first to pop from stack"<<endl<<endl;
cout<<"Push an item first into the stack"<<endl;
cin>>s[0];
cout<<"Your Pushed item: "<<s[0]<<endl;
}else{
cout<<"Invalid Input!!!"<<endl;
}
return 0;
}