-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedQueue.c
More file actions
70 lines (63 loc) · 1.25 KB
/
LinkedQueue.c
File metadata and controls
70 lines (63 loc) · 1.25 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
//
// Created by ys on 2019/4/12.
//
#include "LinkedQueue.h"
#include "../tools.h"
//初始化
void Init(LQueue *Q) {
(*Q) = (LQueue) malloc(sizeof(Queue));
QNOde *q = (QNOde *) malloc(sizeof(QNOde));
q->next = NULL;
(*Q)->rear = (*Q)->front = q;
}
//队判空操作
int Empty(LQueue Q) {
if ((Q->front) == (Q->rear)) {
return true;
} else {
return false;
}
}
//入队操作
void EnQueue(LQueue Q, ElemType e) {
QNOde *q = (QNOde *) malloc(sizeof(QNOde));
if (q == NULL) {
printf("申请空间失败\n");
exit(0);
}
q->data = e;
q->next = NULL;
Q->rear->next = q;
Q->rear = q;
}
//出队操作
ElemType Dequeue(LQueue *Q) {
if (Empty(*Q)) {
printf("队列已空,无法进行出队操作\n");
exit(0);
}
QNOde *q = (*Q)->front->next;
(*Q)->front->next = q->next;
ElemType x = q->data;
free(q);
if ((*Q)->front->next == NULL) {
(*Q)->rear = (*Q)->front;
}
return x;
}
//测试
void testQueue() {
LQueue Q;
Init(&Q);
int bool = Empty(Q);
EnQueue(Q, 1);
bool = Empty(Q);
for (int i = 2; i < 10; ++i) {
EnQueue(Q, i);
}
ElemType x;
for (int j = 0; j < 10; ++j) {
x = Dequeue(&Q);
printf("%d\n",x);
}
}