-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.c
More file actions
142 lines (131 loc) · 2.67 KB
/
queue.c
File metadata and controls
142 lines (131 loc) · 2.67 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "queue.h"
#include <inttypes.h> // PRIu32
#include <stdint.h> // extended integer library
#include <stdio.h> // Print
#include <stdlib.h> // Malloc
// Code adapted from stack.c which is taken from Professor Darrell Long and teaching assistant Eugene
//
// Structure for defining queue data struture
//
struct Queue {
uint32_t head; // new values at head
uint32_t tail; // next item to pop at tail
uint32_t size; // current size
uint32_t capacity; // max size
int64_t *items; // array of items
};
//
// Creates a queue
//
// capacity: max size of the queue
//
Queue *queue_create(uint32_t capacity) {
Queue *q = (Queue *) malloc(sizeof(Queue));
if (q) {
q->head = 0;
q->tail = 0;
q->size = 0;
q->capacity = capacity;
q->items = (int64_t *) calloc(capacity, sizeof(int64_t));
if (!q->items) {
free(q);
q = NULL;
}
}
return q;
}
//
// Deletes a queue
//
// s: a queue to delete
//
void queue_delete(Queue **q) {
if (*q && (*q)->items) {
free((*q)->items);
free(*q);
*q = NULL;
}
return;
}
//
// Returns if queue is empty or not
//
// s: a queue to check
//
bool queue_empty(Queue *q) {
return q->size == 0;
}
//
// Returns if queue is full or not
//
// s: queue to test
//
bool queue_full(Queue *q) {
return q->size == q->capacity;
}
//
// Returns size of the queue
//
// s: the queue to return the size of
//
uint32_t queue_size(Queue *q) {
return q->size;
}
//
// Adds the given item to the bottom of the queue and returns if successful
//
// s: queue to push item to
// x: item to add to queue
//
bool enqueue(Queue *q, int64_t x) {
if (queue_full(q)) {
return false;
}
q->items[q->head] = x;
if (q->head == q->capacity - 1) {
q->head = 0;
} else {
q->head++;
}
q->size++;
return true;
}
//
// Removes first item on the queue and stores returns the
//
// s: the queue to pop from
// x: the address to store the popped value
//
bool dequeue(Queue *q, int64_t *x) {
if (queue_empty(q)) {
return false;
}
*x = q->items[q->tail];
if (q->tail == q->capacity - 1) {
q->tail = 0;
} else {
q->tail++;
}
q->size--;
return true;
}
//
// Prints elements of the queue
//
// s: the queue to print
//
void queue_print(Queue *q) {
printf("[");
uint32_t j = 0;
for (uint32_t i = q->tail; j < q->size; i++, j++) {
if (i == q->capacity) {
i = 0;
}
printf("AT %d:", i);
printf("%" PRId64, q->items[i]);
if (j + 1 != q->size) {
printf(", ");
}
}
printf("]\n");
}