-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathstack.cpp
96 lines (74 loc) · 1.19 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node
{
int data;
node *link;
};
node *root=NULL;
void push()
{
node *temp;
temp=(node*)malloc(sizeof(node));
cout<<"Enter Number:";
cin>>temp->data;
if(root==NULL)
{
root=temp;
temp->link=NULL;
}
else
{
temp->link=root;
root=temp;
}
}
void pop()
{
node *temp;
temp=root;
if(temp==NULL)
{//nothing
}
else
{
root=temp->link;
temp->link=NULL;
free(temp);
}
}
void display()
{
node *temp;
temp=root;
if(temp==NULL)
{cout<<"\n Stack Is Empty\n";
}
else
{
while(temp!=NULL)
{cout<<" ^\n";
cout<<"|"<<temp->data<<"|\n";
temp=temp->link;
}
}
}
int main()
{
int c,l;
display();
cout<<"\n1.Push\n2.Pop\n0.End\n";
cin>>c;
while(c!=0)
{
switch(c)
{
case 1:push();display();cout<<"\n";break;
case 2:pop();display();cout<<"\n";break;
default :break;
}
cout<<"\n1.Push\n2.Pop\n0.End\n";
cin>>c;
}
}