-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue.h
More file actions
104 lines (88 loc) · 1.73 KB
/
Copy pathqueue.h
File metadata and controls
104 lines (88 loc) · 1.73 KB
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
#ifndef _DS_QUEUE_H
#define _DS_QUEUE_H
/* Circular Queue (Index) Data Structure (for array indexing)
* Namespace: queue
*
* Basic operation for implementing a array based queue.
*
* In this version the possible overflow of indexes are not managed.
*/
#include <stddef.h>
#include <stdbool.h>
struct QueueIndex {
size_t size; /* capacity */
size_t head; /* start of the data */
size_t len; /* how many data */
};
typedef struct QueueIndex QueueIndex;
int queue_init(QueueIndex *q, const size_t size)
{
if (q == NULL){
return -1;
}
q->size = size;
q->head = 0;
q->len = 0;
return 0;
}
bool queue_isempty(const QueueIndex *q)
{
if (q == NULL){
return false;
}
return q->len == 0;
}
bool queue_isfull(const QueueIndex *q)
{
if (q == NULL){
return false;
}
return q->len == q->size;
}
size_t queue_length(const QueueIndex *q)
{
if (q == NULL){
return false;
}
return q->len;
}
size_t queue_size(const QueueIndex *q)
{
if (q == NULL){
return false;
}
return q->size;
}
/* return the index for setting the value in the support array.
* -1 in case of overflow
*/
long queue_enqueue(QueueIndex *q)
{
if (q == NULL){
return -1;
}
if (queue_isfull(q)){
return -1;
}
/* circular */
long i = (long)((q->head + q->len) % q->size);
q->len++;
return i;
}
/* return the index for getting the value in the support array.
* -1 in case of underflow
*/
long queue_dequeue(QueueIndex *q)
{
if (q == NULL){
return -1;
}
if (queue_isempty(q)){
return -1;
}
long i = (long)q->head;
q->head = (q->head + 1) % q->size;
q->len--;
return i;
}
#endif