-
Notifications
You must be signed in to change notification settings - Fork 0
/
30_stack_in_linked_list.c
82 lines (68 loc) · 1.51 KB
/
30_stack_in_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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<stdio.h>
#include<stdlib.h>
//26-07-21
struct Node{
int data;
struct Node *next;
};
struct Node *top = NULL;
void linkedlistTraversal(struct Node *ptr){
while (ptr != NULL)
{
printf("Element = %d\n", ptr->data);
ptr = ptr->next;
}
}
int isEmpty(struct Node *top){
if (top == NULL)
{
// printf("!!Empty Linked list warning!!");
return 1;
}
return 0;
}
int isFull(struct Node *top){
struct Node *n = (struct Node *)malloc(sizeof(struct Node));
if (n == NULL)
{
// printf("!!Heap memory Exhaustion warning!!");
return 1;
}
return 0;
}
struct Node *push(struct Node *top, int x){
if (isFull(top))
{
printf("!!Heap memory Exhaustion warning!!\n");
}
struct Node *p = (struct Node *)malloc(sizeof(struct Node));
p->data = x;
p->next = top;
top = p;
return top;
}
//https://www.codewithharry.com/videos/data-structures-and-algorithms-in-hindi-30
int pop(struct Node *tp){
if (isEmpty(tp))
{
printf("!!Heap memory Exhaustion warning!!\n");
}
struct Node *p = tp;
top = tp->next;
int x = p->data;
free(p);
return x;
}
int main(){
top = push(top, 54);
top = push(top, 55);
top = push(top, 56);
top = push(top, 57);
linkedlistTraversal(top);
top = push(top, 69);
linkedlistTraversal(top);
int element = pop(top);
printf("\n popedElement = %d\n", element);
linkedlistTraversal(top);
return 0;
}