-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
101 lines (80 loc) · 2.05 KB
/
Copy pathqueue.c
File metadata and controls
101 lines (80 loc) · 2.05 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* This file contains the definitions of structures and functions implementing
* a simple queue using a linked list.
*/
#include <stdlib.h>
#include <assert.h>
#include "link.h"
#include "queue.h"
/*
* This is the definition of the queue structure. Using a linked list to
* implement a queue requires that we keep track of both the head and the
* tail of the queue.
*/
struct queue {
struct link* head;
struct link* tail;
};
struct queue* queue_create() {
struct queue* queue = malloc(sizeof(struct queue));
assert(queue);
queue->head = NULL;
queue->tail = NULL;
return queue;
}
void queue_free(struct queue* queue) {
assert(queue);
/*
* Here, we're assuming that queue_dequeue() handles freeing the memory
* associated with each dequeued element.
*/
while (!queue_isempty(queue)) {
queue_dequeue(queue);
}
free(queue);
}
int queue_isempty(struct queue* queue) {
assert(queue);
return queue->head == NULL;
}
void queue_enqueue(struct queue* queue, int value) {
assert(queue);
struct link* new_link = malloc(sizeof(struct link));
assert(new_link);
/*
* Fill out the new link at put it at the tail of the list, which represents
* the top of the queue.
*/
new_link->value = value;
new_link->next = NULL;
if (queue->tail) {
queue->tail->next = new_link;
}
queue->tail = new_link;
// If we didn't have a head link before, set the head to this new link, too.
if (!queue->head) {
queue->head = new_link;
}
}
int queue_front(struct queue* queue) {
assert(queue && queue->head);
return queue->head->value;
}
int queue_dequeue(struct queue* queue) {
assert(queue && queue->head);
/*
* Remove the old front element from the list and remember its value before
* we free it.
*/
struct link* dequeued_head = queue->head;
int value = dequeued_head->value;
queue->head = dequeued_head->next;
/*
* If the dequeued head was also the tail, set the tail to NULL.
*/
if (queue->tail == dequeued_head) {
queue->tail = NULL;
}
free(dequeued_head);
return value;
}