-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
60 lines (52 loc) · 1019 Bytes
/
queue.c
File metadata and controls
60 lines (52 loc) · 1019 Bytes
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
#include <stdlib.h>
#include <stddef.h>
#include <stdbool.h>
#include "queue.h"
/*
* creates a new list element
*/
static List* new_node(size_t x) {
List* temp = (List *)malloc(sizeof(List));
temp->value = x;
temp->next = NULL;
return temp;
}
/*
* creates a new, empty queue
*/
Queue* q_init() {
Queue *q = (Queue *)malloc(sizeof(Queue));
q->start = q->end = NULL;
return q;
}
/*
* push an element to the queue
*/
void push(Queue *q, size_t x) {
List *temp = new_node(x);
if (q->end == NULL) {
q->start = q->end = temp;
} else {
q->end->next = temp;
q->end = temp;
}
}
/*
* returns the element from the queue
*/
size_t pop(Queue *q) {
size_t return_value = q->start->value;
List *temp = q->start;
q->start = q->start->next;
if (q->start == NULL) {
q->end = NULL;
}
free(temp);
return return_value;
}
/*
* checks if the queue is empty
*/
bool is_empty(Queue q) {
return q.start == NULL;
}