-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
138 lines (130 loc) · 2.96 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
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
/*
* File: list.c
* Author: Jayson Rhynas, 20299211
* Created: January 14, 2011
*
* Implements doubly-linked lists for the various objects.
*/
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
#include "defenses.h"
#include "enemies.h"
#include "main.h"
/* Creates an empty list and records the type as specified */
List* listCreate (IType type) {
List* list;
list = (List *)malloc(sizeof(List));
list->type = type;
list->size = 0;
list->first = NULL;
list->last = NULL;
return list;
}
/* Adds a node to the end of the list. */
void listAppend (List *list, void *item) {
Node *node;
if (list == NULL || item == NULL) {
return;
}
node = (Node*)malloc(sizeof(Node));
node->item = item;
if (list->type == ENEMY) {
Enemy *e;
e = item;
e->container = node;
} else if (list->type == DEFENSE) {
Defense *d;
d = item;
d->container = node;
} else if (list->type == BULLET) {
Bullet *b;
b = item;
b->container = node;
} else if (list->type == BUTTON) {
Button *b;
b = item;
b->container = node;
}
node->prev = list->last;
node->next = NULL;
if (list->last != NULL) {
list->last->next = node;
}
list->last = node;
if (list->first == NULL) {
list->first = list->last;
}
list->size++;
}
/* Removes the specified node from the given list
Does not check to ensure node is in list. BE CAREFUL */
void listRemove (List *list, Node *node) {
if (list == NULL || node == NULL) {
return;
}
if (node->prev == NULL && node->next == NULL) {
list->first = NULL;
list->last = NULL;
} else if (node->prev == NULL) {
list->first = node->next;
list->first->prev = NULL;
} else if (node->next == NULL) {
list->last = node->prev;
list->last->next = NULL;
} else {
node->prev->next = node->next;
node->next->prev = node->prev;
}
list->size--;
free(node);
}
void listClear (List *list) {
if (list == NULL) {
return;
}
Node *c, *n;
c = list->first;
while(c != NULL) {
n = c->next;
free(c->item);
free(c);
c = n;
}
list->first = NULL;
list->last = NULL;
}
void listDelete(List *list) {
if (list == NULL) {
return;
}
listClear(list);
free(list);
}
void listForEach (List *list, void(*func)(void*)) {
if (list == NULL) {
return;
}
Node *node;
node = list->first;
while(node != NULL) {
func(node->item);
node = node->next;
}
}
void listPrint (List *list) {
if (list == NULL) {
return;
}
Node *node;
node = list->first;
while(node != NULL) {
if (list->type == ENEMY) {
fprintf(stderr, " e ");
} else {
fprintf(stderr, " d ");
}
node = node->next;
}
fprintf(stderr, "\n");
}