-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircqueue.c
100 lines (84 loc) · 1.57 KB
/
circqueue.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
// Harris Ransom
// C Circular Queue w/arrays
// Based on video by Jacob Sorber
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "circqueue.h"
#define QUEUE_SIZE 5
typedef struct myq {
int *values;
int head, tail, n, size;
} queue;
// Initializes queue
void initQueue(queue *q, int maxSize) {
q->size = maxSize;
q->values = malloc(sizeof(int) * q->size);
q->n = 0;
q->head = 0;
q->tail = 0;
}
// Checks if queue is empty
bool isEmpty(queue *q) {
return (q->n == 0) ? true : false;
}
// Checks if queue is full
bool isFull(queue *q) {
return (q->n == q->size) ? true : false;
}
// Frees queue memory
void queueDestroy(queue *q) {
free(q->values);
}
// Adds node to queue
bool enqueue(queue *q, int value) {
if (isFull(q)) return false;
q->values[q->tail] = value;
q->n++;
q->tail++;
q->tail %= q->size;
return true;
}
// Removes node from queue
int dequeue(queue *q) {
int result;
// Check if queue is empty
if (isEmpty(q)) return false;
result = q->values[q->head];
q->head++;
q->head %= q->size;
q->n--;
return result;
}
// Outputs queue
void printQueue(queue *q) {
if (isEmpty(q)) {
printf("<Empty>\n");
}
else {
for (int i = 0; i < q->n; i++) {
printf("%d\t%d\n", i+1, q->values[i]);
}
printf("end\n");
}
}
// MAIN
/*
int main() {
// Initializes queue
queue q1;
initQueue(&q1, QUEUE_SIZE);
printQueue(&q1);
// Adds items to queue
enqueue(&q1, 56);
enqueue(&q1, 78);
enqueue(&q1, 23);
enqueue(&q1, 988);
enqueue(&q1, 13);
enqueue(&q1, 2);
printQueue(&q1);
// Frees queue
queueDestroy(&q1);
return 0;
}
*/