-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_movers.c
173 lines (162 loc) · 4.25 KB
/
list_movers.c
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
/*Marwan Mostafa mam024 11305332*/
/* Addison McAuley, amm637, 11295940
* Ghanim Jamil, ghj729, 11298913
*/
#include <stdio.h>
#include <stdlib.h>
#include <list.h>
#include <string.h>
/*
* Returns the current number of items in list
* @param list: list we want to find the count of
* @return the number of items in our list
*/
int ListCount(LIST* list) {
if (list == NULL) {
char* msg = "Error, cannot get count of a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else {
return list->count;
}
}
/*
* Moves cursor to the first item in our list, and returns that item
* @param list: The list we are navigating
* @return the item in the first position of our list, if no head
* (empty list), return null
*/
void* ListFirst(LIST* list) {
if (list == NULL) {
char* msg = "Error, cannot navigate a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->head == NULL) {
return NULL;
}
else {
list->curr = list->head;
return list->curr->item;
}
}
/*
* Moves cursor to the last position in list and returns the item
* @param list: list we are navigating
* @return the item in the last position of our list. If tail is null
* (empty list), just return null pointer
*/
void* ListLast(LIST* list) {
if (list == NULL) {
char* msg = "Error, cannot navigate a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->tail == NULL) {
return NULL;
}
else {
list->curr = list->tail;
return list->curr->item;
}
}
/*
* Attemps to move the cursor to the next item in our list and returns the
* item
* @param list: the list we are navigating
* @return the item at our new cursor positon, or null if we attempt to
* navigate past the end of the list. If the cursor is null, will move
* forward to the head of the list
*/
void* ListNext(LIST* list) {
if (list == NULL) {
char* msg = "Error, cannot navigate a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->curr == NULL) {
list->curr = list->head;
if (list->head == NULL) {
return NULL;
}
else {
return list->head->item;
}
}
else if (list->curr->next == NULL) {
return NULL;
}
else {
list->curr = (NODE*) list->curr->next;
return list->curr->item;
}
}
/*
* Attemps to move the cursor to the previous item in our list and returns
* the item
* @param list: the list we are navigating
* @return the item at our new cursor positon, or NULL if we attempt to move
* backwards past the start of list. If our cursor is null, will move back
* to the tail item
*/
void* ListPrev(LIST* list) {
if (list == NULL) {
char* msg = "Error, cannot navigate a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->curr == NULL) {
return list->tail;
}
else if (list->curr->prev == NULL) {
return NULL;
}
else {
list->curr = (NODE*) list->curr->prev;
return list->curr->item;
}
}
/*
* Returns the item at our current cursor position
* @param list: the list we are retrieving the item from
* @return the item at our current cursor position, if cursor is
* NULL, simply return NULL pointer
*/
void* ListCurr(LIST* list) {
if (list == NULL) {
char* msg = "Error, cannot get the cursor of a null list\n";
fwrite(msg, 1, strlen(msg), stderr);
exit(ERROR_EXIT);
}
else if (list->curr == NULL) {
return NULL;
}
else {
return list->curr->item;
}
}
/*
* Navigates our list, and searches for a match with comparison_arg, according
* to the comparison algorithm defined by comp. When found, our cursor will
* remain at the item that matched comparison_arg. If we reach the end of the
* list, cursor remains at end
* @param list: the list we will be searching
* @param comp: a pointer to a function that determines whether the current
* item matches comparison_arg
* @param comparison_arg: item we will be searching for
* @return the item that matches, or NULL if comparison_arg is not in the list
*/
void* ListSearch(LIST* list, Comparator comp, void* comparisonArg) {
if ((*comp) (ListCurr(list), comparisonArg) == 0) {
return ListCurr(list);
}
else {
while(ListNext(list) != NULL) {
if ((*comp)(ListCurr(list), comparisonArg) == 0) {
return ListCurr(list);
}
}
}
return NULL;
}