-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
56 lines (47 loc) · 1.11 KB
/
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
48
49
50
51
52
53
54
55
56
// Harris Ransom
// C Queue
// Based on video by Jacob Sorber
// Note: You can also implement a queue using an array
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#include "queue.h"
// Initializes queue
void initQueue(queue *q) {
q->head = NULL;
q->tail = NULL;
}
// Adds node to queue
bool enqueue(queue *q, int value) {
// Create new node
node *newnode = malloc(sizeof(node));
if (newnode == NULL) return false;
newnode->value = value;
newnode->next = NULL;
// If tail exists, connect new node to tail
if (q->tail != NULL) {
q->tail->next = newnode;
}
q->tail = newnode;
// Check that the head is correct
if (q->head == NULL) {
q->head = newnode;
}
return true;
}
// Removes node from queue
int dequeue(queue *q) {
// Check if queue is empty
if (q->head == NULL) return QUEUE_EMPTY;
// Save head of queue
node *tmp = q->head;
int result = tmp->value;
// Remove from list
q->head = q->head->next;
if (q->head == NULL) {
q->tail = NULL;
}
free(tmp);
return result;
}