-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
87 lines (66 loc) · 1.58 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
/*
* Adapted from: https://www3.cs.stonybrook.edu/~skiena/392/programs/
*/
#include "queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void init_queue(struct Queue *q) {
q->first = 0;
q->last = QUEUESIZE-1;
q->count = 0;
}
void enqueue(struct Queue *q, int x) {
if (q->count >= QUEUESIZE)
printf("Warning: queue overflow enqueue x=%d\n",x);
else {
q->last = (q->last+1) % QUEUESIZE;
q->q[ q->last ] = x;
q->count = q->count + 1;
}
}
int dequeue(struct Queue *q) {
int x;
if (q->count <= 0) printf("Warning: empty queue dequeue.\n");
else {
x = q->q[ q->first ];
q->first = (q->first+1) % QUEUESIZE;
q->count = q->count - 1;
}
return(x);
}
int peek(struct Queue *q) {
int x;
if (q->count <= 0) printf("Warning: empty queue peek.\n");
else {
x = q->q[ q->first ];
}
return(x);
}
bool empty(struct Queue *q) {
if (q->count <= 0) return (1);
else return (0);
}
void print_queue(struct Queue *q) {
int i;
i = q->first;
while (i != q->last) {
printf("%2d ", q->q[i]);
i = (i+1) % QUEUESIZE;
}
printf("%2d ",q->q[i]);
printf("\n");
}
char* get_queue_string(struct Queue *q) {
char* out = malloc(sizeof(char) * 255);
sprintf(out, " ");
int i;
i = q->first;
while (i != q->last) {
sprintf(out + strlen(out), "%2d ", q->q[i]);
i = (i+1) % QUEUESIZE;
}
sprintf(out + strlen(out), "%2d ",q->q[i]);
sprintf(out + strlen(out), "\n");
return out;
}