-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathList.c
More file actions
114 lines (98 loc) · 1.59 KB
/
List.c
File metadata and controls
114 lines (98 loc) · 1.59 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
#include "List.h"
Node* Node_create(void* ptr)
{
Node* n = malloc(sizeof(Node));
n->element = ptr;
n->next = NULL;
return n;
}
void Node_delete(Node* n)
{
free(n);
}
List* List_create()
{
List* l = malloc(sizeof(List));
l->first = NULL;
return l;
}
void List_delete(List* l)
{
Node* next;
Node* akt = l->first;
while(akt != NULL)
{
next = akt->next;
free(akt);
akt = next;
}
free(l);
}
void List_add(List* list, void* e)
{
Node* node = Node_create(e);
if(list->first == NULL)
{
list->first = node;
}
else
{
node->next = list->first;
list->first = node;
}
}
void* List_get(List* list, uint index)
{
Node* n = list->first;
int i;
for(i = 0; i < index; i++)
{
n = n->next;
}
return n->element;
}
void* List_remove(List* list)
{
if(list->first == NULL)
{
return NULL;
}
Node* res = list->first;
list->first = list->first->next;
return res->element;
}
uint List_count(List* l)
{
uint r = 0;
Node* n = l->first;
while(n != NULL)
{
r++;
n = n->next;
}
return r;
}
int List_isEmpty(List* l)
{
return l->first == NULL;
}
void* ListIterator_next(ListIterator* l)
{
if(l->akt == NULL)
{
return NULL;
}
void* r = l->akt->element;
l->akt = l->akt->next;
return r;
}
ListIterator* List_getIterator(List* l)
{
ListIterator* r = malloc(sizeof(ListIterator));
r->akt = l->first;
return r;
}
void ListIterator_delete(ListIterator* l)
{
free(l);
}