-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstack_linked_list.c
64 lines (58 loc) · 902 Bytes
/
stack_linked_list.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
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
} *top = NULL;
void Push(int x)
{
struct Node *t;
t = (struct Node *)malloc(sizeof(struct Node));
if (t == NULL) // stack is full or heap is full
printf("stack is full\n");
else
{
t->data = x;
t->next = top;
top = t;
}
}
int Pop()
{
struct Node *t;
int x = -1;
if (top == NULL)
printf("Stack is Empty");
else
{
t = top;
top = top->next;
x = t->data;
free(t);
}
return x;
}
void Display()
{
struct Node *p;
p = top;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
}
int main()
{
Push(10);
Push(20);
Push(30);
Push(40);
Display();
printf("\npopped : %d", Pop());
return 0;
}
// Output :
// 40 30 20 10
// popped : 40