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

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

void pushq(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 popq(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);
}