summaryrefslogtreecommitdiff
path: root/que.c
blob: 9ebee7145c787ed505f11efe73ee7de014ae28b1 (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
#include <u.h>
#include <libc.h>

typedef struct Node Node;
struct Node {
	int n;
	Node *next;
};

Node *head, *tail;

void *
emalloc(ulong n)
{
	void *p;

	p = malloc(n);
	if(p == nil)
		sysfatal("malloc: %r");
	memset(p, 0, n);
	setmalloctag(p, getcallerpc(&n));
	return p;
}

void
put(int n)
{
	if(tail == nil){
		tail = emalloc(sizeof(Node));
		tail->n = n;
		head = tail;
		return;
	}
	tail->next = emalloc(sizeof(Node));
	tail = tail->next;
	tail->n = n;
}

int
get(void)
{
	Node *nn;
	int n;

	if(head == nil)
		return -1;
	nn = head->next;
	n = head->n;
	free(head);
	head = nn;
	return n;
}

int
isemtpy(void)
{
	return head == nil;
}

void
main()
{
	int i;

	for(i = 0; i < 10; i++)
		put(i*3);
	while(!isemtpy())
		print("%d\n", get());
	exits(nil);
}