-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack Link.cpp
More file actions
118 lines (104 loc) · 1.4 KB
/
Stack Link.cpp
File metadata and controls
118 lines (104 loc) · 1.4 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* different operations on stack using linked list */
#include<stdio.h>
#include<stdlib.h>
struct stack
{
int data;
struct stack *next;
};
int main()
{
struct stack *s;
int x,n,elt;
void push(struct stack **,int );
int empty(struct stack *);
int pop(struct stack **);
int stacktop(struct stack *);
s=NULL;
do
{
printf("MENU\n");
printf("1.Push\n2.Pop\n3.Stacktop\n4.Exit\n");
printf("enter choice ");
scanf("%d",&n);
switch(n)
{
case 1:
printf("enter the no to be pushed ");
scanf("%d",&elt);
push(&s,elt);
break;
case 2:
if(empty(s))
printf("UNDERFLOW\n");
else
x=pop(&s);
printf("popped element = %d\n",x);
break;
case 3:
if(empty(s))
printf("UNDERFLOW\n");
else
x=stacktop(s);
printf("top element = %d\n",x);
break;
}
}
while(n<4);
return(0);
}
int empty(struct stack *ps)
{
if(ps==NULL)
{
return(1);
}
else
{
return(0);
}
}
void push(struct stack **ps,int x)
{
struct stack *p;
p=(struct stack *)malloc(sizeof(struct stack));
p->data=x;
if(p==NULL)
{
printf("OVERFLOW\n");
}
else
{
if(*ps==NULL)
{
p->next=NULL;
*ps=p;
}
else
{
p->next=*ps;
*ps=p;
}
}
}
int pop(struct stack **ps)
{
struct stack *h;
int x;
h=*ps;
x=h->data;
if(h->next==NULL)
{
*ps=NULL;
}
else
{
*ps=h->next;
}
free(h);
return(x);
}
int stacktop(struct stack *ps)
{
return(ps->data);
}