-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.c
76 lines (75 loc) · 1.25 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
73
74
75
76
#include "queue.h"
#include <assert.h>
#include <malloc.h>
#include <stdio.h>
qNode * BuyNode(DataType data)
{
qNode * new = (qNode*)malloc(sizeof(qNode));
if (NULL == new) {
printf("分配失败");
return NULL;
}
new->data = data;
new->next = NULL;
return new;
}
void InitQueue(LQueue *queue)
{
assert(queue);
//创建头结点
qNode * head= BuyNode(0);
queue->front = head;
queue->tail = head;
}
void PushQueue(LQueue *queue, DataType data)
{
qNode *new = BuyNode(data);
queue->tail->next = new;
queue->tail = new;
}
void PopQueue(LQueue * queue)
{
qNode *pDel = queue->front->next;
//不是空队列
if (pDel) {
//除头结点只有一个结点
if (NULL == pDel->next) {
queue->tail = queue->front;
}
queue->front->next = pDel->next;
}
}
DataType QueueFront(LQueue* queue)
{
if (queue->front->next) {
return queue->front->next->data;
}
return queue->front->data;
}
int QueueSize(LQueue* queue)
{
int count = 0;
qNode * pcur = queue->front->next;
while (pcur) {
count++;
pcur = pcur->next;
}
return count;
}
int QueueEmpyt(LQueue* queue)
{
if (queue->front == queue->tail) {
return 1;
}
return 0;
}
void DestoryQueue(LQueue** queue)
{
if (*queue != NULL) {
while ((*queue)->front) {
PopQueue(*queue);
}
free(*queue);
*queue = NULL;
}
}