-
Notifications
You must be signed in to change notification settings - Fork 2
/
ADT_Linklist_node_push_pop.c
66 lines (58 loc) · 1.29 KB
/
ADT_Linklist_node_push_pop.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
// author: jaydattpatel
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node * next;
};
struct Node* top = NULL;
void Traversal(struct Node *tp)
{
while (tp != NULL)
{
printf("Element: %d\n", tp->data);
tp = tp->next;
}
}
struct Node* push(struct Node* tp, int x)
{
struct Node* n = (struct Node*) malloc(sizeof(struct Node));
n->data = x;
n->next = tp;
tp = n;
return tp;
}
int pop(struct Node* tp)
{
if(tp == NULL)
{
printf("Stack Underflow\n");
return 0;
}
else
{
struct Node* n = tp;
top = (tp)->next;
int x = n->data;
free(n);
printf("Popped element is %d\n", x);
return x;
}
}
int main()
{
top = push(top, 78);
top = push(top, 7);
top = push(top, 8);
top = push(top, 10);
top = push(top, 15);
top = push(top, 55);
printf("-----------------Before pop:\n");
Traversal(top);
pop(top);
pop(top);
printf("----------------After pop:\n");
Traversal(top);
return 0;
}