-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLAB10.C
More file actions
127 lines (93 loc) · 1.73 KB
/
LAB10.C
File metadata and controls
127 lines (93 loc) · 1.73 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
116
117
118
119
120
121
122
123
124
125
126
127
// Linked list as stack implementation is quite
// simple and easy to implement
// Push operation is nothing but adding@front
// pop operation is delete@front
#include<alloc.h>
#define MAX 5
struct node
{
int data;
struct node *link;
};
struct node * getnode()
{
return ( struct node *) malloc( sizeof( struct node ));
}
struct node * push( struct node * , int );
struct node * pop( struct node *);
void display( struct node * );
void main( )
{
struct node *H;
int c = 0 , x , choice;
H = NULL;
while(1)
{
clrscr();
printf("1. Push \n");
printf("2. Pop \n");
printf("3. Display \n");
printf("4. Exit \n");
printf("Choice = ");
scanf("%d", &choice);
switch( choice )
{
case 1:
if( c == MAX )
printf("Stack overflow\n");
else
{
printf("Enter element= ");
scanf("%d",&x);
H = push( H , x );
c++;
}
break;
case 2:
if( c == 0 )
printf("Stack empty \n");
else
{
H = pop( H );
c--;
}
break;
case 3:
if( c == 0 )
printf("OOPS !!!! nothing to display \n");
else
display( H );
break;
case 4:
exit(0);
}
getch();
}
}
struct node *push( struct node *H , int data)
{
struct node *newnode;
newnode = getnode();
newnode->data = data;
newnode->link = H;
H = newnode;
return H;
}
struct node *pop( struct node *H)
{
struct node *T;
printf("Poped element = %d\n", H->data);
T = H;
H = H->link;
free( T);
return H;
}
void display( struct node *T )
{
printf("Content of the stack \n");
while( T!= NULL)
{
printf("%d\n", T->data);
T = T->link;
}
}