-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
72 lines (62 loc) · 1.33 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
#include <stdlib.h>
#include <stdio.h>
#include <float.h>
#include "queue.h"
queue_t* new_queue(int size) {
queue_t* q = malloc(sizeof(queue_t));
q->array = calloc(size, sizeof(float));
q->ptr = q -> array;
q->size = size;
q->num_elements = 0;
return q;
}
void shift_ptr(queue_t* q, int amount) {
int i = q->ptr - q->array;
int j = (i + amount) % q->size;
q->ptr = q->array + j;
}
void delete_queue(queue_t* q) {
free(q->array);
free(q);
}
void enqueue(queue_t* q, float value) {
q->ptr[0] = value;
shift_ptr(q, 1);
if (q->num_elements == q->size) {
puts("Warning: overwrote the tail element of the queue.");
}
if (q->num_elements < q->size) {
q->num_elements++;
}
}
int is_full(queue_t* q) {
return q->num_elements == q->size;
}
float dequeue(queue_t* q) {
shift_ptr(q, -1);
return q->ptr[0];
q->num_elements--;
}
void get_extrema(queue_t* q,
float* min_ptr,
float* max_ptr) {
float min = FLT_MAX;
float max = FLT_MIN;
for (int i = -q->num_elements; i < 0; i++) {
float val = q->ptr[i];
if (val > max) {
max = val;
}
if (val < min) {
min = val;
}
}
*min_ptr = min;
*max_ptr = max;
}
void print_queue(queue_t* q) {
for (int i = 0; i < q->num_elements; i++) {
printf("[%d]: %f\n", i, q->array[i]);
}
printf("\n");
}