-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
76 lines (73 loc) · 1.19 KB
/
stack.cpp
File metadata and controls
76 lines (73 loc) · 1.19 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
#include<iostream>
using namespace std;
struct stack
{
int data;
struct stack *next;
};
class stac
{
struct stack *top;
public:
stac()
{
top=NULL;
}
void push(int n)
{
struct stack *ptr=new stack;
ptr->data=n;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
//cout<<ptr->data;
}
void pop()
{
if(top==NULL)
{
cout<<"Underflow"<<endl;
return ;
}
struct stack *temp;
temp=top;
top=top->next;
cout<<"\nPopped element :"<<temp->data<<endl;
delete temp;
}
void show()
{
struct stack* s=top;
while(s!=NULL)
{
cout<<s->data<<"\t";
s=s->next;
}
}
};
int main()
{
stac s;
int ch,n;
while(1)
{
cout<<"[1].push\n[2].pop\n[3].show\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter n :";
cin>>n;
s.push(n);
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
}
}
return 0;
}