-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_link_list.c
More file actions
106 lines (94 loc) · 1.63 KB
/
Copy pathstack_link_list.c
File metadata and controls
106 lines (94 loc) · 1.63 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
#include <stdio.h>
#include <stdlib.h>
// unfin
typedef struct stack_node
{
int val;
struct stack_node *next;
} stack;
// functions
stack *create_node(int val);
int null_check(stack *s);
stack *push(stack *s, int val);
int pop(stack *h);
void free_stack(stack *h);
void print_stack(stack *h);
stack *create_node(int val)
{
stack *newnode = (stack *)malloc(sizeof(stack));
newnode->val = val;
newnode->next = NULL;
return newnode;
}
int null_check(stack *s)
{
if (s == NULL)
{
printf("Stack does not exist\n");
return 1;
}
return 0;
}
stack *push(stack *h, int val)
{
if (h == NULL)
{
stack *newnode = create_node(val);
return newnode;
}
stack *newnode = create_node(val);
stack *cur = h;
while (cur->next != NULL)
{
cur = cur->next;
}
cur->next = newnode;
return h;
}
int pop(stack *h)
{
if (null_check(h) != 0)
return -1;
stack *cur = h;
while (cur->next != NULL)
{
cur = cur->next;
}
int temp = cur->val;
free(cur);
return temp;
}
void free_stack(stack *h)
{
if (null_check(h) != 0)
return;
stack *cur = h;
while (cur->next != NULL)
{
stack *temp = cur->next;
free(cur);
cur = temp;
}
free(cur);
}
void print_stack(stack *h)
{
if (null_check(h) != 0)
return;
stack *cur = h;
while (cur != NULL)
{
printf("%d ", cur->val);
cur = cur->next;
}
}
int main()
{
stack *h = create_node(1);
push(h, 2);
push(h, 5);
pop(h);
push(h, 7);
print_stack(h);
return 0;
}