Modestus Moon OS  R4
CS 450 project
linked_list.c
Go to the documentation of this file.
1 #include "linked_list.h"
2 
4 {
5 #ifdef LL_IS_ARRAY_BACKED
6  int i;
7  for(i=0; i < MAX_NUM_OF_LL_NODES; i++)
8  {
9  list->backingArray[i].data=0;
10  list->backingArray[i].inUse=0;
11  list->backingArray[i].next=0;
12  list->backingArray[i].prev=0;
13  list->backingArray[i].parentList = list;
14  }
15 
16  list->head.parentList = list;
17  list->head.inUse=1;
18 
19  list->tail.parentList = list;
20  list->tail.inUse=1;
21 #endif
22 
23  list->head.data=0;
24  list->head.next =
25  list->head.prev = &list->tail;
26 
27  list->tail.data=0;
28  list->tail.next =
29  list->tail.prev = &list->head;
30 
31  list->length = 0;
32  list->insertCompFunc = 0;
33  list->freeNodeFunc = 0;
34  list->searchCompFunc = 0;
35 }
36 
37 node_t * makeNewNode(linkedList_t *list, void *data)
38 {
39 #ifdef LL_IS_ARRAY_BACKED
40  int i;
41  for(i=0; i < MAX_NUM_OF_LL_NODES; i++)
42  {
43  if(!list->backingArray[i].inUse)
44  {
45  list->backingArray[i].inUse = 1;
46  list->numNodesInUse++;
47  list->backingArray[i].data = data;
48  return &(list->backingArray[i]);
49  }
50  }
51  return 0;
52 #else
53  (void)list;
54  node_t * newNode = sys_alloc_mem(sizeof(node_t));
55  newNode->data = data;
56  newNode->next=0;
57  newNode->prev=0;
58  return newNode;
59 
60 #endif
61 }
62 
63 void setInsertComparisonFunction(linkedList_t* list, int (*newCompFunc)(void *, void *))
64 {
65  list->insertCompFunc = newCompFunc;
66 }
67 
68 void setFreeFunction(linkedList_t *list, int (*newFreeFunc)(node_t *))
69 {
70  list->freeNodeFunc = newFreeFunc;
71 }
72 
73 void setSearchComparisonFunction(linkedList_t *list, int (*newSearchFunc)(void*, void*))
74 {
75  list->searchCompFunc = newSearchFunc;
76 }
77 
78 node_t *insertAfterNode(node_t * preceedingNode, node_t *newNode)
79 {
80 #ifdef LL_IS_ARRAY_BACKED
81  if(preceedingNode->parentList != newNode->parentList)
82  {
83  //if the node to insert is not apart of the list to insert after, the function moveNodeToNewList makes a
84  //new node in the parent list of the preceedingNode and recursively calls this function with the newly moved node
85  return insertAfterNode(preceedingNode,
86  moveNodeToNewList(newNode, preceedingNode->parentList));
87  }
88 #endif
89 
90  newNode->next = preceedingNode->next;
91  newNode->prev = preceedingNode;
92 
93  newNode->next->prev = newNode;
94  newNode->prev->next = newNode;
95 
96  return newNode;
97 }
98 
99 node_t *insertBeforeNode(node_t *nodeToFollow, node_t * newNode)
100 {
101 #ifdef LL_IS_ARRAY_BACKED
102  if(nodeToFollow->parentList != newNode->parentList)
103  {
104  //if the node to insert is not apart of the list to insert after, the function moveNodeToNewList makes a
105  //new node in the parent list of the preceedingNode and recursively calls this function with the newly moved node
106  return insertBeforeNode(nodeToFollow,
107  moveNodeToNewList(newNode, nodeToFollow->parentList));
108  }
109 #endif
110 
111  newNode->next = nodeToFollow;
112  newNode->prev = nodeToFollow->prev;
113 
114  newNode->next->prev = newNode;
115  newNode->prev->next = newNode;
116 
117  return newNode;
118 }
119 
120 node_t *removeNode(node_t * nodeToRemove)
121 {
122  if(nodeToRemove)
123  {
124  if(nodeToRemove->prev){nodeToRemove->prev->next = nodeToRemove->next;}
125  if(nodeToRemove->next){nodeToRemove->next->prev = nodeToRemove->prev;}
126  nodeToRemove->next = 0;
127  nodeToRemove->prev = 0;
128  }
129  return nodeToRemove;
130 }
131 
132 node_t *moveNodeToNewList(node_t * nodeToMove, linkedList_t *newList)
133 {
134 #ifdef LL_IS_ARRAY_BACKED
135  node_t * newNodeInList = makeNewNode(newList, nodeToMove->data);
136  if(!newNodeInList)
137  {
138  return 0;
139  }
140 
141  removeNode(nodeToMove);
142 
143  if(nodeToMove->parentList->freeNodeFunc)
144  {
145  (*(nodeToMove->parentList->freeNodeFunc))(nodeToMove);
146  }
147  return newNodeInList;
148 #else
149  node_t* removedNode = removeNode(nodeToMove);
150  return insertNode(newList, removedNode);
151 #endif
152 }
153 
154 node_t * searchList(linkedList_t *listToSearch, void *data)
155 {
156  int (*compFunctCpy)(void*,void*) = (listToSearch->searchCompFunc) ?
157  listToSearch->searchCompFunc :
158  listToSearch->insertCompFunc;
159  if(compFunctCpy)
160  {
161  node_t * itNode = listToSearch->head.next;
162  for(; itNode != &(listToSearch->tail); itNode = itNode->next)
163  {
164  if(!(*(compFunctCpy))(itNode->data, data))
165  {
166  return itNode;
167  }
168  }
169  }
170  return 0;
171 }
172 
174 {
175  //if newNode is not valid, do not insert any node
176  if(!newNode)
177  {
178  return 0;
179  }
180 
181 #ifdef LL_IS_ARRAY_BACKED
182  if(newNode->parentList != list)
183  {
184  //if the node to insert is not apart of the list to insert after, the function moveNodeToNewList makes a
185  //new node in the parent list of the preceedingNode and recursively calls this function with the newly moved node
186  return insertNode(list, moveNodeToNewList(newNode, list));
187  }
188 #endif
189 
190  if(list->insertCompFunc)
191  {
192  node_t * itNode = list->head.next;
193  for(; itNode != &(list->tail); itNode = itNode->next)
194  {
195  int result = ((*(list->insertCompFunc))(itNode->data, newNode->data));
196  if(result > 0)
197  {
198  list->length++;
199  return insertBeforeNode(itNode, newNode);
200  }
201  }
202  }
203  list->length++;
204  return insertBeforeNode(&(list->tail), newNode);
205 }
206 
207 void setPrintFunction(linkedList_t *list, void (*newPrintFunc)(void *))
208 {
209  list->printNodeFunc = newPrintFunc;
210 }
211 
213 {
214  if(list->printNodeFunc)
215  {
216  node_t * itNode = list->head.next;
217  if(itNode == &(list->tail))
218  {
219  printf("%s","<List is Empty>\r\n");
220  }
221  for(; itNode != &(list->tail); itNode = itNode->next)
222  {
223  list->printNodeFunc(itNode->data);
224  }
225  }else
226  {
227  printf("No Search Function provided for List!%s\r\n"," ");
228  }
229 }
230 
231 int compFunction(void *nodeData1, void* nodeData2)
232 {
233  //return strcmp((const char*)(nodeData1), (const char*)(nodeData2));
234  (void)nodeData1; (void)nodeData2;
235  return -1;
236 }
237 
238 int searchcompFunction(void *nodeData1, void* nodeData2)
239 {
240  return strcmp((const char*)(nodeData1), (const char*)(nodeData2));
241  //(void)nodeData1; (void)nodeData2;
242  //return -1;
243 }
244 
245 void testPrintFunc(void* nodeData)
246 {
247  printf("List Test Print: data is string: %s\r\n", (const char*)nodeData);
248 }
249 
250 void listTest()
251 {
252  linkedList_t list1;
253  linkedList_t list2;
254 
255  initLinkedList(&list1);
256  initLinkedList(&list2);
257 
261 
262  insertNode(&list1, makeNewNode(&list2, (void*)"test3"));
263  insertNode(&list1, makeNewNode(&list2, (void*)"test1"));
264  insertNode(&list1, makeNewNode(&list2, (void*)"test5"));
265  insertNode(&list1, makeNewNode(&list2, (void*)"test2"));
266  insertNode(&list1, makeNewNode(&list2, (void*)"test7"));
267  insertNode(&list1, makeNewNode(&list2, (void*)"test4"));
268  insertNode(&list1, makeNewNode(&list2, (void*)"test8"));
269  insertNode(&list1, makeNewNode(&list2, (void*)"test3"));
270 
271 
272  // node_t * nodeFound = searchList(&list1, (void*)"test3");
273 
274  node_t * nodeFound = searchList(&list1, (void*)"test5");
275  printf("looking for node whose data is: %s\r\nsearch return: %d\r\n", "test5", (int)nodeFound);
276 
277  if(nodeFound)
278  {
279  printf("found node:: data string = %s\r\n", (const char*)nodeFound->data);
280  }
281 
282  printList(&list1);
283 }
void setInsertComparisonFunction(linkedList_t *list, int(*newCompFunc)(void *, void *))
takes in a function pointer and sets the library shared comparison pointer
Definition: linked_list.c:63
void * sys_alloc_mem(u32int size)
Definition: mpx_supt.c:33
node_t * insertAfterNode(node_t *preceedingNode, node_t *newNode)
insert a node into the parent list of the preceedingNode after preceedingNode
Definition: linked_list.c:78
typedef of linked list node (see s_ll_node).
void setPrintFunction(linkedList_t *list, void(*newPrintFunc)(void *))
sets the function whose job it is to print the list to the screen.
Definition: linked_list.c:207
node_t * moveNodeToNewList(node_t *nodeToMove, linkedList_t *newList)
If a node needs to move lists and lists are array backed, instead of just moving preceeding and sucee...
Definition: linked_list.c:132
node_t * insertBeforeNode(node_t *nodeToFollow, node_t *newNode)
insert a node into the parent list of the nodeToFollow before nodeToFollow
Definition: linked_list.c:99
int searchcompFunction(void *nodeData1, void *nodeData2)
Definition: linked_list.c:238
#define printf(format,...)
printf is simply a wrapper macro around sprintf with built in terminal print builtin needs to be a ma...
Definition: string.h:112
void setFreeFunction(linkedList_t *list, int(*newFreeFunc)(node_t *))
sets the function whose job it is to free the node&#39;s memory
Definition: linked_list.c:68
typedef of linked list (see s_ll).
void printList(linkedList_t *list)
test function to show list functionality. uses const char* as test data
Definition: linked_list.c:212
void testPrintFunc(void *nodeData)
Definition: linked_list.c:245
int compFunction(void *nodeData1, void *nodeData2)
Definition: linked_list.c:231
void setSearchComparisonFunction(linkedList_t *list, int(*newSearchFunc)(void *, void *))
sets the function whose job it is to compare the input data with the data in the list. the first void parameter will always be the data contained in the list, the second will be the data passed to the search function.
Definition: linked_list.c:73
node_t * searchList(linkedList_t *listToSearch, void *data)
goes through the list using the list&#39;s comparison function to find the node which matches the data...
Definition: linked_list.c:154
node_t * removeNode(node_t *nodeToRemove)
will remove the node from the list. the list hierarchy will change to reflect the missing node...
Definition: linked_list.c:120
void initLinkedList(linkedList_t *list)
initilize list and the optional array that backs the list
Definition: linked_list.c:3
#define MAX_NUM_OF_LL_NODES
Definition: linked_list.h:22
int strcmp(const char *s1, const char *s2)
strcmp compares two strings.
Definition: string.c:77
void listTest()
test the linked_list class with const char* data and output to screen the results of the test ...
Definition: linked_list.c:250
node_t * insertNode(linkedList_t *list, node_t *newNode)
Will insert the node into the list. This function will attempt to call the list&#39;s insert comparison f...
Definition: linked_list.c:173
node_t * makeNewNode(linkedList_t *list, void *data)
get new node, allocate a new node or find an unused node from the list&#39;s pool
Definition: linked_list.c:37