-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
103 lines (92 loc) · 1.74 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* list.c
* GameJam
* January 28, 2012
* Brandon Surmanski
*/
#include <stdlib.h>
#include <string.h>
#include "list.h"
typedef struct Node{
struct Node *prev;
struct Node *next;
void *data;
} Node;
struct List{
size_t eleSz;
Node *first;
Node *last;
Node *itter;
};
List *list_new(size_t eleSz)
{
List *l = malloc(sizeof(List));
l->eleSz = eleSz;
l->first = 0;
l->itter = 0;
return l;
}
void list_add(List *l, void *ele)
{
Node *n = malloc(sizeof(Node));
n->prev = 0;
n->next = 0;
n->data = malloc(l->eleSz);
memcpy(n->data, ele, l->eleSz);
if(!l->first){
l->first = n;
l->last = n;
} else {
l->last->next = n;
n->prev = l->last;
l->last = n;
}
}
void *list_itter(List *l)
{
if(!l->itter)
l->itter = l->first;
else
l->itter = l->itter->next;
return l->itter ? l->itter->data : 0;
}
void list_removeLastItter(List *l)
{
Node *prev = l->itter->prev;
Node *next = l->itter->next;
Node *n = l->itter;
//l->itter = l->first;
if(prev){
prev->next = next;
l->itter = prev;
} else {
l->first = next;
if (!next){
l->itter = 0;
}
}
if(next){
next->prev = prev;
}
//FIXME haha! memory leak on particles...
//free(n->data);
//free(n);
}
void list_clear(List *l)
{
l->first = 0;
l->last = 0;
l->itter = 0;
}
void list_delete(List *l)
{
Node *n = l->first;
Node *next = n->next;
while(n){
free(n->data);
free(n);
n = next;
next = n->next;
}
free(l);
}