aboutsummaryrefslogtreecommitdiffstats
path: root/utils/Burg/list.c
blob: d3eefa27d0992e738eaa32f5dd0058e5a32030fe (plain)
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
char rcsid_list[] = "$Id$";

#include "b.h"

IntList
newIntList(x, next) int x; IntList next;
{
	IntList l;

	l = (IntList) zalloc(sizeof(*l));
	assert(l);
	l->x = x;
	l->next = next;

	return l;
}

List
newList(x, next) void *x; List next;
{
	List l;

	l = (List) zalloc(sizeof(*l));
	assert(l);
	l->x = x;
	l->next = next;

	return l;
}

List
appendList(x, l) void *x; List l;
{
	List last;
	List p;

	last = 0;
	for (p = l; p; p = p->next) {
		last = p;
	}
	if (last) {
		last->next = newList(x, 0);
		return l;
	} else {
		return newList(x, 0);
	}
}

void
foreachList(f, l) ListFn f; List l;
{
	for (; l; l = l->next) {
		(*f)(l->x);
	}
}

void
reveachList(f, l) ListFn f; List l;
{
	if (l) {
		reveachList(f, l->next);
		(*f)(l->x);
	}
}

int
length(l) List l;
{
	int c = 0;

	for(; l; l = l->next) {
		c++;
	}
	return c;
}