-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlinkedList.c
More file actions
220 lines (192 loc) · 6.1 KB
/
Copy pathlinkedList.c
File metadata and controls
220 lines (192 loc) · 6.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "linkedList.h"
/* ════════════════════════════════════════════
Internal helper – allocate and fill a node
════════════════════════════════════════════ */
static ListNode *nodeCreate(NodeData data) {
ListNode *n = (ListNode *)malloc(sizeof(ListNode));
if (!n) {
fprintf(stderr, "[linkedList] malloc failed\n");
return NULL;
}
n->data = data;
n->next = NULL;
return n;
}
/* ════════════════════════════════════════════
Lifecycle
════════════════════════════════════════════ */
/* Initialise an empty list. Must be called before any other function. */
void listInit(LinkedList *list) {
if (!list) return;
list->head = NULL;
list->size = 0;
}
/* Free every node and reset the list to empty. */
void listDestroy(LinkedList *list) {
if (!list) return;
ListNode *cur = list->head;
while (cur) {
ListNode *tmp = cur->next;
free(cur);
cur = tmp;
}
list->head = NULL;
list->size = 0;
}
/* ════════════════════════════════════════════
Insert
════════════════════════════════════════════ */
/* Insert at the front – O(1).
Returns 0 on success, -1 on allocation failure. */
int listPushFront(LinkedList *list, NodeData data) {
if (!list)
return -1;
ListNode *n = nodeCreate(data);
if (!n)
return -1;
n->next = list->head;
list->head = n;
list->size++;
return 0;
}
/* ════════════════════════════════════════════
Utility
════════════════════════════════════════════ */
/* Print every node to stdout. */
void listPrint(const LinkedList *list) {
if (!list || !list->head) {
printf("[empty list]\n");
return;
}
printf("List (%zu nodes):\n", list->size);
ListNode *cur = list->head;
size_t i = 0;
while (cur) {
printf(" [%zu] type=%c x=%d y=%d w=%d h=%d text=%s\n",
i++, cur->data.type, cur->data.r, cur->data.c, cur->data.w, cur->data.h, cur->data.text);
cur = cur->next;
}
}
/* Append to the back – O(n).
Returns 0 on success, -1 on allocation failure. */
int listPushBack(LinkedList *list, NodeData data) {
if (!list)
return -1;
ListNode *n = nodeCreate(data);
if (!n)
return -1;
if (!list->head) {
list->head = n;
} else {
ListNode *cur = list->head;
while (cur->next)
cur = cur->next;
cur->next = n;
}
list->size++;
return 0;
}
/* Insert before the node currently at `index` (0-based) – O(n).
Appends when index >= size.
Returns 0 on success, -1 on failure. */
int listInsertAt(LinkedList *list, size_t index, NodeData data) {
if (!list)
return -1;
/* Delegate edge cases */
if (index == 0)
return listPushFront(list, data);
if (index >= list->size)
return listPushBack (list, data);
ListNode *n = nodeCreate(data);
if (!n)
return -1;
ListNode *cur = list->head;
for (size_t i = 0; i < index - 1; i++)
cur = cur->next;
n->next = cur->next;
cur->next = n;
list->size++;
return 0;
}
/* ════════════════════════════════════════════
Remove
════════════════════════════════════════════ */
/* Remove the first node – O(1).
Returns 0 on success, -1 if the list is empty. */
int listPopFront(LinkedList *list) {
if (!list || !list->head)
return -1;
ListNode *tmp = list->head;
list->head = tmp->next;
free(tmp);
list->size--;
return 0;
}
/* Remove the last node – O(n).
Returns 0 on success, -1 if the list is empty. */
int listPopBack(LinkedList *list)
{
if (!list || !list->head)
return -1;
if (!list->head->next) { /* single node */
free(list->head);
list->head = NULL;
} else {
ListNode *cur = list->head;
while (cur->next->next)
cur = cur->next;
free(cur->next);
cur->next = NULL;
}
list->size--;
return 0;
}
/* Remove the node at `index` (0-based) – O(n).
Returns 0 on success, -1 on out-of-range or empty list. */
int listRemoveAt(LinkedList *list, size_t index) {
if (!list || !list->head || index >= list->size)
return -1;
if (index == 0)
return listPopFront(list);
ListNode *cur = list->head;
for (size_t i = 0; i < index - 1; i++) {
cur = cur->next;
}
ListNode *tmp = cur->next;
cur->next = tmp->next;
free(tmp);
list->size--;
return 0;
}
/* Return a pointer to the node at `index`, or NULL if out-of-range. */
ListNode *listGetAt(const LinkedList *list, size_t index) {
if (!list || index >= list->size)
return NULL;
ListNode *cur = list->head;
for (size_t i = 0; i < index; i++) {
cur = cur->next;
}
return cur;
}
/* Reverse the list in-place – O(n). */
void listReverse(LinkedList *list) {
if (!list || list->size < 2)
return;
ListNode *prev = NULL;
ListNode *cur = list->head;
while (cur) {
ListNode *next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
list->head = prev;
}
/* Return the number of nodes. */
size_t listSize(const LinkedList *list) {
return list ? list->size : 0;
}
/* Return 1 if the list is empty, 0 otherwise. */
int listIsEmpty(const LinkedList *list) {
return (!list || list->size == 0) ? 1 : 0;
}