-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueues.c
105 lines (88 loc) · 2.3 KB
/
queues.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
101
102
103
104
105
#include "queues.h"
// implementation of a standard queue data structure using a singly linked list for educational purposes
// create queue from array
Queue* q_create(void* arr[], const size_t n, const size_t typeSize)
{
Queue* queue = (Queue*)malloc(sizeof(Queue));
queue->items = LibLinkedList.create(arr, n, typeSize);
return queue;
}
int q_free(Queue* queue, void (*free_item)(void*))
{
LibLinkedList.free(queue->items, free_item);
free(queue);
queue = NULL;
return EXIT_SUCCESS;
}
// add an element to the queue
int q_enqueue(Queue* queue, const void* value)
{
// use linked list library to insert item to back
return LibLinkedList.insert(queue->items, value, -1);
}
int q_enqueue_int(Queue* queue, int value)
{
return q_enqueue(queue, &value);
}
int q_enqueue_str(Queue* queue, const char* value)
{
return q_enqueue(queue, (void*)value);
}
int q_enqueue_flt(Queue* queue, float value)
{
return q_enqueue(queue, &value);
}
int q_enqueue_dbl(Queue* queue, double value)
{
return q_enqueue(queue, &value);
}
int q_enqueue_chr(Queue* queue, char value)
{
return q_enqueue(queue, &value);
}
// return an element from the front of the queue
// pass null to out if you don't need to receive the item
int q_dequeue(Queue* queue, void* out)
{
if (q_empty(queue))
{
return EXIT_FAILURE;
}
if (out != NULL)
{
memcpy(out, queue->items->head->value, queue->items->dataSize);
}
// delete element at head
return LibLinkedList.delete(queue->items, 0);
}
int q_peek(Queue* queue, void* out)
{
if (q_empty(queue) || out == NULL)
{
EXIT_FAILURE;
}
memcpy(out, queue->items->head->value, queue->items->dataSize);
return EXIT_SUCCESS;
}
bool q_empty(Queue* queue)
{
return queue->items == NULL || queue->items->head == NULL;
}
int q_print(Queue* queue, void (*print)(const void*))
{
return LibLinkedList.print(queue->items, print);
}
const struct LibQueue_l LibQueue = {
.create = q_create,
.enqueue = q_enqueue,
.enqueue_int = q_enqueue_int,
.enqueue_str = q_enqueue_str,
.enqueue_chr = q_enqueue_chr,
.enqueue_dbl = q_enqueue_dbl,
.enqueue_flt = q_enqueue_flt,
.print = q_print,
.dequeue = q_dequeue,
.peek = q_peek,
.is_empty = q_empty,
.free = q_free
};