about summary refs log tree commit diff
path: root/usth/ICT2.1/labwork/3/Ex2.c
blob: 49be21ba73bea8a2b83e3e5d20831c5dbc0f2e22 (plain) (blame)
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
/*
 * Add hard-coded names to a queue a print them to stdout.
 * This is free and unencumbered software released into the public domain.
 */

#include <stdio.h>
#include <stdlib.h>

#include "construct.h"

typedef struct {
	construct *front;
	construct *rear;
} queue;

queue *mkq()
{
	queue *q = malloc(sizeof(queue));
	q->front = q->rear = NULL;
}

int qempty(queue *q)
{
	return q->front == NULL;
}

void qpush(queue *q, void *item)
{
	if (qempty(q))
		q->front = q->rear = cons(item, NULL);
	else
		q->rear = q->rear->cdr = cons(item, NULL);
}

void *qpop(queue *q)
{
	if (qempty(q))
		return NULL;
	void *first = car(q->front);
	construct *rest = cdr(q->front);
	free(q->front);
	q->front = rest;
	return first;
}

int main()
{
	queue *q = mkq();	
	qpush(q, "Mahathir Mohamad");
	qpush(q, "Elizabeth II");
	qpush(q, "Sheikh Sabah Al-Ahmad Al-Jaber Al-Sabah");
	qpush(q, "Paul Biya");
	qpush(q, "Michel Aoun");
	qpush(q, "Mahmoud Abbas");
	qpush(q, "Francis");

	while (!qempty(q))
		puts(qpop(q));

	return 0;
}