-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
71 lines (54 loc) · 1.49 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <err.h>
#include "list.h"
List *list_new(void) {
List *list = malloc(sizeof(List));
list->size = 0;
list->items = NULL;
return list;
};
void list_free(List *list) {
if (list->items != NULL) {
free(list->items);
}
free(list);
}
int list_length(List *list) { return list->size; }
void list_append(List *list, void *item) {
list->size++;
list->items = realloc(list->items, list->size * sizeof(item));
list->items[list->size - 1] = item;
}
/* Insert item as the first element in list. */
void list_push(List *list, void *item) {
list->size++;
void **new_items = malloc(list->size * sizeof(item));
memcpy(new_items + 1, list->items, (list->size - 1) * sizeof(item));
if (list->items != NULL) {
free(list->items);
}
list->items = new_items;
list->items[0] = item;
}
/* Remove the last item from the list, and return it.
*/
void *list_pop(List *list) {
void *value = list_get(list, list->size - 1);
list->size--;
list->items = realloc(list->items, list->size * sizeof(value));
return value;
}
void *list_get(List *list, int index) { return list->items[index]; }
void list_set(List *list, int index, void *value) {
if (index < 0 || index > list->size) {
warnx("Index %d is out of bounds!", index);
}
else if (index == list->size) {
list_append(list, value);
}
else {
list->items[index] = value;
}
}