-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.c
More file actions
140 lines (109 loc) · 2.79 KB
/
Copy pathList.c
File metadata and controls
140 lines (109 loc) · 2.79 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
#ifndef __LIST_C__
#define __LIST_C__
#include "List.h"
ListNode* ListGet(List* list, int index) {
ListNode* curr = NULL;
// List too short.
if (NULL == list) return (NULL);
if (list->size <= index) return (NULL);
// Advance to index.
curr = list->first;
while (index-- > 0) curr = curr->next;
return (curr);
}
ListNode* ListLast(List* list) {
return (ListGet(list, list->size - 1));
}
// List Modification Methods.
int ListInsert(List* list, ListNode* node, int index) {
ListNode* curr = NULL;
if (NULL == list) return 0;
if (NULL == node) return 0;
if (index < 0) return 0;
// For indexes larger than size, append to the end.
if (index > list->size) {
list->last->next = node;
node->prev = list->last;
node->next = NULL;
// Find the node at index.
} else {
curr = list->first;
while (index-- > 0) curr = curr->next;
node->next = curr->next;
curr->next = node;
node->prev = curr;
node->next->prev = node;
}
return (++list->size);
}
ListNode* ListRemove(List* list, int index) {
ListNode* removed = NULL;
ListNode* curr = NULL;
if (NULL == list) return (NULL);
if (index >= list->size) return (0);
// Get the node pointing to the deletion spot.
curr = list->first;
while (index-- > 1) curr = curr->next;
// Get the actual node to remove.
if (list->size == 1) {
removed = curr;
list->first = NULL;
list->last = NULL;
} else {
removed = curr->next;
curr->next = removed->next;
curr->next->prev = curr;
}
list->size--;
removed->next = NULL;
removed->prev = NULL;
return (removed);
}
void ListClear(List* list) {
ListNode* curr = NULL;
if (NULL != list) {
while (list->size > 0) {
curr = ListRemove(list, 0);
free(curr);
}
}
}
int ListAppend(List* list, ListNode* node) {
return (ListInsert(list, node, list->size));
}
// List Information Methods.
List* ListCopy(List* list) {
ListNode* curr = NULL;
ListNode* node = NULL;
// Copy list core.
List* copy = (List*)malloc(sizeof(List));
copy->size = list->size;
// Copy each node.
curr = list->first;
while (NULL != curr) {
// Get the last node.
node = copy->first;
while (NULL != node->next) node = node->next;
// Add the last node.
copy->last = (ListNode*)malloc(sizeof(ListNode));
node->next = copy->last;
copy->last->prev = node;
copy->last->next = NULL;
copy->last->data = curr->data;
// Get the next node.
curr = curr->next;
}
return (copy);
}
int ListCount(List* list) {
if (NULL == list) return (0);
return (list->size);
}
void ListReverse(List* list) {
if (NULL == list) return;
}
// List Data Methods.
void ListSort(List* list, int* comp(void*)) {
if (NULL == list) return;
}
#endif // __LIST_C__