-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.c
115 lines (91 loc) · 2.67 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
/**
* @brief Returns a pointer to a queue struct after allocating memory
* for it and initialising each of its fields.
*
* @return struct queue* Pointer to queue struct
*/
struct queue* initialise_queue() {
// Allocate memory for queue
struct queue* queue = (struct queue*) malloc(sizeof(struct queue));
if (queue == NULL) {
fprintf(stderr, "Unable to allocate memory for queue\n");
exit(1);
}
// Initialise size and pointers to first and last elements
queue->size = 0;
queue->first = NULL;
queue->last = NULL;
return queue;
}
/**
* @brief Frees memory allocated to each node in the queue as well
* as the queue itself.
*
* @param queue Pointer to queue to free
*/
void free_queue(struct queue* queue) {
// Continue dequeing and freeing each node until queue is empty
while (is_empty(queue) == 0) {
struct node* node = dequeue(queue);
free((void*) node->packet);
free(node);
}
// Free memory allocated to queue itself
free(queue);
}
/**
* @brief Returns whether a given queue is empty.
*
* @param queue Pointer to queue to check
* @return int 1 if the queue is empty, 0 otherwise
*/
int is_empty(struct queue* queue) {
return (queue->first == NULL);
}
/**
* @brief Inserts a given packet at the back of the queue.
*
* @param queue Pointer to queue in which to insert
* @param packet Packet to insert into queue
*/
void enqueue(struct queue* queue, struct packet* packet) {
// Create new node to store packet
struct node* new = (struct node*) malloc(sizeof(struct node));
new->packet = packet;
new->next = NULL;
// Update pointers to first and last elements of queue
if (is_empty(queue) == 1) {
queue->first = new;
} else {
queue->last->next = new;
}
queue->last = new;
// Increment variable tracking size of queue
queue->size++;
}
/**
* @brief Removes and returns the packet at the front of the given queue.
*
* @param queue Pointer to queue from which to remove
* @return struct node* Packet removed from the front of the queue
*/
struct node* dequeue(struct queue* queue) {
// Ensure queue is not already empty
if (is_empty(queue) == 1) {
fprintf(stderr, "Unable to dequeue items from an empty queue\n");
exit(1);
}
// Update first pointer
struct node* node = queue->first;
queue->first = queue->first->next;
// Update last pointer if queue is now empty
if (queue->first == NULL) {
queue->last = NULL;
}
// Decrement variable tracking size of queue
queue->size--;
return node;
}