-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
75 lines (75 loc) · 1.38 KB
/
stack.cpp
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
#include<iostream>
using namespace std;
//linked list implementation of stack
struct node{
int data;
struct node *link;
}*top=NULL,*temp,*trav;
class stack{
public:
void push(int value){
temp=new node;
if(temp==NULL)
cout<<"\nNo memory, Overflow";
else if(top==NULL){
trav->data=value;
trav->link=NULL;
}
else{
temp->data=value;
top->link=temp;
temp->link=NULL;
top=temp;
}
}
void pop(){
if(top->link==NULL)
cout<<"\nEmpty Stack, Underflow!";
else{
temp=top;
top=top->link;
delete temp;
}
}
void top(){
cout<<"\nThe topmost element is:\t"<<top->data;
}
void display(){
cout<<"\nThe current elements are:\n";
struct node *ptr=*top;
while (ptr->link!=NULL){
cout<<ptr->data<<"\t";
ptr=ptr->link;
}
cout<<ptr->data<<"\n";
}
};
int main(){
stack obj;
int ch,n;
char c;
cout<<"1:Push\n2:Pop\n3:Top\n4:Display";
do{
cout<<"\nEnter choice:\t";
cin ch;
switch(ch){
case 1:cout<<"\nEnter value of element:\t";
cin>>n;
obj.push(n);
break;
case 2:obj.pop();
break;
case 3:obj.top();
break;
case 4:obj.display();
break;
default:
cout<<"\nInvalid choice!";
break;
}
cout<<"\nDo you want to continue: (y or n)\t";
cin>>c;
}while(c==y);
cout<<"\nThank you!";
return 0;
}