-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstack.cpp
More file actions
97 lines (97 loc) · 1.82 KB
/
stack.cpp
File metadata and controls
97 lines (97 loc) · 1.82 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
91
92
93
94
95
96
97
#include<bits/stdc++.h>
using namespace std;
struct node{
int data;
struct node *next;
}node;
struct node *top=NULL;
void create(){
if(top!=NULL){
cout<<"stack already has been created"<<endl;
}
else{
cout<<"creating a new stack "<<endl;
struct node *p=new struct node;
cout<<"enter data"<<endl;
cin>>p->data;
p->next=NULL;
top=p;
}
}
void push(){
if (top==NULL){
cout<<"creating a new stack"<<endl;
create();
}
else{
struct node *p=new struct node;
cout<<"enter new data"<<endl;
cin>>p->data;
p->next=top;
top=p;
}
}
void pop(){
if(top==NULL){
cout<<"underflow"<<endl;
}
else{
struct node *q=top;
top=q->next;
q->next=NULL;
free(q);
}
}
void peek(){
if(top==NULL){
cout<<"underflow"<<endl;
}
else{
struct node *q=top;
cout<<"first element of stack "<<q->data<<endl;
}
}
void display(){
if(top==NULL){
cout<<"underflow"<<endl;
}
else{
cout<<"Values in stack"<<endl;
struct node *q=top;
while(q!=NULL){
cout<<q->data<<" ";
q=q->next;
}
}
cout<<endl;
}
int main(){
create();
char c='y';
int k;
while(c=='Y'||c=='y'){
cout<<"1.push\n2.pop\n3.peek\n4.display\n";
cin>>k;
switch (k)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
peek();
break;
case 4:
display();
break;
default:
cout<<"wrong input"<<endl;
break;
}
cout<<"enter y/n to contiue/exit"<<endl;
cin>>c;
}
return 0;
}