-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathqueue.c
More file actions
93 lines (72 loc) · 1.48 KB
/
queue.c
File metadata and controls
93 lines (72 loc) · 1.48 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
//
// queue.c
// RegexCompiler
//
// Created by 臻华的Macbook pro on 2018/11/23.
// Copyright © 2018 臻华的Macbook pro. All rights reserved.
//
#include "queue.h"
Queue *InitQueue(void)
{
Queue *queue = malloc(sizeof(Queue));
if (!queue) QueueError();
queue->front = queue->rear = InitQNode();
return queue;
}
QNode *InitQNode(void)
{
QNode *node = malloc(sizeof(QNode));
if (!node) QueueError();
node->data = NULL;
node->next = NULL;
return node;
}
void EnQueue(Queue *q, const void *ele)
{
q->rear->next = InitQNode();
q->rear = q->rear->next;
q->rear->data = ele;
}
void *DeQueue(Queue *q)
{
if (q->front == q->rear) QueueEmptyError();
QNode *t = q->front->next;
void *data = (void *)t->data;
q->front->next = t->next;
if (!t->next) q->rear = q->front;
free(t);
t = NULL;
return data;
}
void *GetHead(const Queue *q)
{
if (q->front == q->rear) QueueEmptyError();
void *data = (void *)q->front->next->data;
return data;
}
bool EmptyQueue(const Queue *q)
{
return q->front == q->rear;
}
void QueueError(void)
{
printf("队列内存分配失败\n");
exit(1);
}
void QueueEmptyError(void)
{
printf("队列已空\n");
exit(1);
}
void FreeQueue(Queue *q)
{
QNode *node = q->front;
while (node) {
QNode *t = node;
free(t);
t = NULL;
node = node->next;
}
free(q);
q = NULL;
}