aboutsummaryrefslogtreecommitdiff
path: root/src/lib/q.c
blob: 1f5a87134f5b6ec65686a4afe8049289e8a2cd73 (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* 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);
}