-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
175 lines (156 loc) · 3.79 KB
/
Copy pathlist.c
File metadata and controls
175 lines (156 loc) · 3.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
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
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
/*
* List implementation
*/
/*
* Returns a newly created, empty list.
*/
list_t *list_create(void)
{
list_t *myList = malloc(sizeof(list_t));
myList->head = NULL;
myList->numItems = 0;
return myList;
}
/*
* Frees the list; list and nodes, but not the items it holds.
*/
void list_destroy(list_t *list)
{
listnode_t *currentNode = list->head;
listnode_t *nextNode;
while(1) {
nextNode = currentNode->next;
if (nextNode == NULL) {
free(currentNode);
free(list);
if (currentNode != NULL || list != NULL)
printf("list destroy memory free failed\n");
break;
}
else {
free(currentNode);
if (currentNode != NULL)
printf("list destroy memory free failed\n");
currentNode = nextNode;
}
}
}
/*
* Adds an item first in the provided list.
*/
void list_addfirst(list_t *list, void *item)
{
listnode_t *node = malloc(sizeof(listnode_t));
node->item = item;
listnode_t *oldHead = list->head;
list->head = node;
if (list->numItems == 0){
node->next = NULL;
}
else {
node->next = oldHead;
}
list->numItems += 1;
}
/*
* Adds an item last in the provided list.
*/
void list_addlast(list_t *list, void *item)
{
listnode_t *currentNode = list->head;
listnode_t *node = malloc(sizeof(listnode_t));
node->next = NULL;
node->item = item;
while(1) {
//No items in list
if (list->numItems == 0){
list->head = node;
list->numItems ++;
break;
} else if (currentNode->next == NULL){
currentNode->next = node;
list->numItems ++;
break;
} else {
currentNode = currentNode->next;
}
}
}
/*
* Removes an item from the provided list, only freeing the node.
*/
void list_remove(list_t *list, void *item)
{
listnode_t *prevNode = NULL;
listnode_t *currentNode = list->head;
while(1) {
if (currentNode->item == item) {
if (currentNode == list->head) {
list->head = currentNode->next;
free(currentNode);
list->numItems --;
break;
}
else {
prevNode->next = currentNode->next;
free(currentNode);
list->numItems --;
break;
}
}
else {
prevNode = currentNode;
currentNode = currentNode->next;
}
}
}
/*
* Return the number of items in the list.
*/
int list_size(list_t *list)
{
return(list->numItems);
}
/*
* Iterator implementation
*/
/*
* Return a newly created list iterator for the given list.
*/
list_iterator_t *list_createiterator(list_t *list)
{
list_iterator_t *iter = malloc(sizeof(list_iterator_t));
iter->list = list;
iter->next = list->head;
return iter;
}
/*
* Free the memory for the given list iterator.
*/
void list_destroyiterator(list_iterator_t *iter)
{
free(iter);
}
/*
* Move iterator to next item in list and return current.
*/
void *list_next(list_iterator_t *iter)
{
void *cItem = iter->next->item;
iter->next = iter->next->next;
//(printf("%p\n", iter->next);
//if list is at last position, reset
if(iter->next == NULL)
list_resetiterator(iter);
return cItem;
}
/*
* Let iterator point to first item in list again.
*/
void list_resetiterator(list_iterator_t *iter)
{
iter->next = iter->list->head;
}