-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.c
More file actions
64 lines (57 loc) · 1.42 KB
/
list.c
File metadata and controls
64 lines (57 loc) · 1.42 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
/* An implementation of "list.h" interface
based on resizeable arrays
(ensures a constant amortized cost)
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "list.h"
struct _list {int next; int size; int *elements;};
static void *protect(void *p){
if (p==NULL) {
printf("ERROR: unexpected null pointer. No memory left ?\n");
exit(1);
}
return p;
}
list empty() {
list res=protect(malloc(sizeof(struct _list)));
res->next=0;
res->size=2;
res->elements=protect(malloc(res->size*sizeof(int)));
return res;
}
int length(list l){
assert (l != NULL);
return l->next;
}
void append(list l, int v){
int n;
assert (l != NULL);
n=l->next++;
if (n >= l->size) {
l->size *= 2;
l->elements = protect(realloc(l->elements, l->size*sizeof(int)));
}
assert (n < l->size);
l->elements[n] = v;
printf("#%d = %d\n", n+1, v);
}
int pick(list l, int i){
assert (l != NULL);
if (i < 1 || i > l->next) {
printf("ERROR: #%d unassigned\n", i);
exit(1);
}
return l->elements[i-1];
}
void destruct(list *l){
list x;
assert (l != NULL);
x=*l;
*l=NULL;
if (x == NULL)
return;
free(x->elements);
free(x);
}