-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinked_list.c
More file actions
152 lines (126 loc) · 2.93 KB
/
linked_list.c
File metadata and controls
152 lines (126 loc) · 2.93 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "linked_list.h"
struct Node* init_linked_list_node(int value)
{
struct Node* head = NULL;
head = malloc(sizeof(*head));
head->value = value;
head->next = NULL;
return head;
}
void free_linked_list_node(struct Node* node)
{
free(node);
}
struct Node* append_linked_list(struct Node* head, struct Node* item)
{
struct Node* original = head;
/*
Walk through list until we hit the end,
then link item to end of list.
*/
do
{
if (head->next == NULL)
{
head->next = item;
break;
}
} while ((head = head->next));
return original;
}
struct Node* prepend_linked_list(struct Node* head, struct Node* item)
{
/*
Prepending can basically be boiled down to
appending the head of the linked list to the end of
the item linked list
*/
return append_linked_list(item, head);
}
struct Node* insert_linked_list(struct Node* head, struct Node* item, int index)
{
struct Node* original = head;
struct Node* next = NULL;
/*
If index is 0, we just prepend to list
*/
if (index == 0)
return prepend_linked_list(head, item);
/*
Walk the linked list until we hit our index
*/
for (int i = 0; i < index - 1; i++)
{
if (head->next == NULL)
break;
head = head->next;
}
/*
Append the next node of the linked list to the
tail of the new linked list.
*/
next = head->next;
head->next = item;
append_linked_list(head->next, next);
return original;
}
struct Node* pop_linked_list(struct Node* head)
{
struct Node* previous = NULL;
/*
Walk the linked list until the tail,
then unlink the tail and return it.
*/
do
{
if (head->next == NULL)
{
if (previous)
previous->next = NULL;
return head;
}
else
previous = head;
} while ((head = head->next));
return NULL;
}
struct Node* delete_linked_list_node_index(struct Node* head, int index)
{
struct Node* original = head;
struct Node* previous = NULL;
/*
If index == 0, make new head the next entry
and return new head.
*/
if (index == 0)
{
original = head->next;
free_linked_list_node(head);
return original;
}
/*
Walk the linked list until the index is reached.
*/
for (int i = 0; i < index - 1; i++)
{
if (head->next == NULL)
break;
previous = head;
head = head->next;
}
/*
Link the previous node to the next node and
free the current node.
*/
previous->next = head->next;
free_linked_list_node(head);
return original;
}
void print_linked_list(struct Node* head)
{
int i = 0;
do
{
printf("%d:\t%d\n", i++, head->value);
} while ((head = head->next));
}