-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
87 lines (73 loc) · 1.26 KB
/
List.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
#include "List.h"
#include <stdlib.h>
void ListInitialize(List* _list)
{
_list->head = NULL;
_list->size = 0;
}
void ListAdd(List* _list, voidPtr_t _value)
{
ListNode* head = _list->head;
while(head && head->next)
{
head = head->next;
}
ListNode* listNode = malloc(sizeof(ListNode));
if(listNode)
{
listNode->next = NULL;
listNode->value = _value;
if(!head)
{
_list->head = listNode;
}
else
{
head->next = listNode;
}
_list->size++;
}
}
voidPtr_t ListGet(const List* _list, uint32_t _index)
{
uint32_t index = 0;
ListNode* node = _list->head;
while(node && index < _list->size)
{
if(index == _index)
{
return node->value;
}
node = node->next;
++index;
}
return NULL;
}
voidPtr_t ListRemove(List* _list, uint32_t _index)
{
uint32_t index = 0;
ListNode* node = _list->head;
ListNode* prevNode = NULL;
while (node && index < _list->size)
{
if (index == _index)
{
voidPtr_t value = node->value;
if(prevNode)
{
prevNode->next = node->next;
}
else
{
_list->head = node->next;
}
_list->size--;
free(node);
return value;
}
prevNode = node;
node = node->next;
++index;
}
return NULL;
}