aboutsummaryrefslogtreecommitdiff
path: root/src/lib/q.c
blob: 2ade143489b599921202ab82333467e60c19c06d (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
#include <lib/q.h>
#include <lib/mem.h>

struct Q_base* new_q()
{
	struct Q_base* q = (struct Q_base*)malloc(sizeof(struct Q_base));
	q->next = 0;
	q->last = 0;
	return q;
}

void push_q(struct Q_base* qb, void* val)
{
	struct Q* n = (struct Q*)malloc(sizeof(struct Q));
	n->next = 0;
	n->data = val;
	if (qb->last != 0)
		qb->last->next = n;
	else {
		qb->next = n;
	}
	qb->last = n;
}

void pop_q(struct Q_base* qb)
{
	if (qb->next == 0)
		return;
	if (qb->next == qb->last) {
		free(qb->next->data);
		free(qb->next);
		qb->next = 0;
		qb->last = 0;
		return;
	}
	struct Q* t = qb->next;
	qb->next = qb->next->next;
	free(t->data);
	free(t);
}

unsigned long length_q(struct Q_base* qb)
{
	unsigned long length = 0;
	if(qb->next == 0)
		return length;
	length++;
	struct Q* q = qb->next;
	while (q != qb->last) {
		length++;
		q = q->next;
	}
	return length;
}