-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
47 lines (38 loc) · 881 Bytes
/
queue.c
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
/* TACHE Robert-Andrei - 312CD */
#include "tree.h"
#include "queue.h"
Queue* initQueue() {
Queue *q = (Queue*) malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
return q;
}
List createList(Tree value) {
List head = malloc(sizeof(struct list));
head->value = value;
head->next = NULL;
return head;
}
int isEmptyQueue(Queue *q) {
return q->front == NULL;
}
void enqueue(Queue *q, Tree node) {
if (q->front == NULL) {
q->front = q->rear = createList(node);
} else {
List list = createList(node);
q->rear->next = list;
q->rear = list;
}
}
Tree dequeue(Queue *q) {
if (q->front == NULL) return NULL;
Tree result = q->front->value;
List tmp = q->front;
q->front = q->front->next;
if (tmp->next == NULL) {
q->rear = NULL;
}
free(tmp);
return result;
}