forked from fgrandoinf/centrality-measures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.cpp
73 lines (56 loc) · 1.42 KB
/
queue.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"
void QueueInit(queue *queue, int maxSize) {
queueElement *newContents;
newContents = (queueElement *) malloc(sizeof(queueElement) * maxSize);
if (newContents == NULL) {
fprintf(stderr, "Insufficient memory to initialize queue.\n");
exit(1);
}
queue->contents = newContents;
queue->front = 0;
queue->count = 0;
queue->maxSize = maxSize;
}
void QueueDestroy(queue *queue) {
free(queue->contents);
}
void QueueEnter(queue *Q, queueElement element) {
int newElementIndex;
if (Q->count >= Q->maxSize) {
fprintf(stderr, "QueueEnter on Full Queue.\n");
exit(1); /* Exit program, returning error code. */
}
/*
* Calculate index at which to put
* next element.
*/
newElementIndex = (Q->front + Q->count) % Q->maxSize;
Q->contents[newElementIndex] = element;
Q->count++;
}
queueElement QueueDelete(queue *Q) {
queueElement oldElement;
if (Q->count <= 0) {
fprintf(stderr, "QueueDelete on Empty Queue.\n");
exit(1); /* Exit program, returning error code. */
}
/* Save the element so we can return it. */
oldElement = Q->contents[Q->front];
/*
* Advance the index of the front,
* making sure it wraps around the
* array properly.
*/
Q->front++;
Q->front %= Q->maxSize;
Q->count--;
return oldElement;
}
int QueueIsEmpty(queue *queue) {
return queue->count <= 0;
}
int QueueIsFull(queue *queue) {
return queue->count >= queue->maxSize;
}