-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.c
More file actions
60 lines (54 loc) · 1.05 KB
/
Queue.c
File metadata and controls
60 lines (54 loc) · 1.05 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
#include "Queue.h"
#include <stdlib.h>
//[head,tile)
void QueuePushfront(Queue q, queueitem item)
{
if (q->head == 0)
q->head = q->capbility - 1;
else
q->head -= 1;
q->items[q->head] = item;
}
void QueuePushback(Queue q, queueitem item)
{
q->items[q->tile] = item;
if (q->tile == q->capbility - 1)
q->tile = 0;
else
q->tile += 1;
}
void QueuePopback(Queue q)
{
if (q->tile == 0)
q->tile = q->capbility - 1;
else
q->tile -= 1;
}
void QueuePopfront(Queue q)
{
if (q->head == q->capbility - 1)
q->head = 0;
else
q->head += 1;
}
queueitem QueueBack(Queue q)
{
if (q->tile == 0)
return q->items[q->capbility - 1];
else
return q->items[q->tile - 1];
}
Queue NewQueue(const int capbility)
{
Queue re = malloc(sizeof(struct _queue));
re->capbility = capbility;
re->items = (queueitem *)malloc(sizeof(queueitem) * capbility);
re->head = 0;
re->tile = 0;
return re;
}
void FreeQueue(Queue q)
{
free(q->items);
free(q);
}