-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.c
119 lines (107 loc) · 2.1 KB
/
stack.c
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<stdio.h>
#include<stdlib.h>
struct node
{
int item;
struct node * next;
}* top;
int IsEmpty()
{
if(top==NULL)
return 0;
else
return 1;
}
int Push()
{
int x;
scanf("%d",&x);
struct node * node0=(struct node *)malloc(sizeof(struct node));
if(node0!=NULL)
{
node0->item=x;
node0->next=top;
top=node0;
return 0;
}
else
return 1;
}
int Top()
{
if(top==NULL)
{
return 1;
}
printf("%d\n",top->item);
return 0;
}
int Pop()
{
if(top==NULL)
{
return 1;
}
printf("%d\n",top->item);
struct node * temp=top;
top=top->next;
free(temp);
return 0;
}
int Display()
{
int err=IsEmpty(top);
if(err==0)
return 1;
else
{
struct node * cur_ptr;
cur_ptr=top;
while(cur_ptr!=NULL)
{
printf("%d\n",cur_ptr->item);
cur_ptr=cur_ptr->next;
}
return 0;
}
}
int main()
{
int x=0,error_code=-1,var;
top=NULL;
do
{
scanf("%d",&x);
switch(x)
{
case 0 : var=IsEmpty();
if(var==0)
printf("Empty\n");
else
printf("Non Empty\n");
break;
case 1 : error_code=Push();
if(error_code==1)
{
printf("Push Failure\n");
exit(1);
}
break;
case 2 : error_code=Pop();
if(error_code==1)
{
printf("Pop Failure\n");
exit(1);
}
break;
case 3 : error_code=Top();break;
case 4 : error_code=Display();
if(error_code==1)
printf("Empty List\n");
break;
case 5 : exit(0);break;
default: exit(0);
}
} while(1);
return 0;
}