-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkQueue.c
More file actions
121 lines (112 loc) · 2.36 KB
/
LinkQueue.c
File metadata and controls
121 lines (112 loc) · 2.36 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "define.h"
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
/*
|-------------------------------------------------------------------------------------------------
|队列的链式存储结构
|-------------------------------------------------------------------------------------------------
|入队列的时间复杂度为O(1)
|出队列的时间复杂度为O(1)
|-------------------------------------------------------------------------------------------------
*/
/**
* 申请新的节点
*/
ListNode static *applyNewNode()
{
PListNode new = (ListNode *)malloc(sizeof(ListNode));
if(new == NULL){
printf("Apply a new node failed\n");
exit(-1);
}
new->next = NULL;
return new;
}
/**
* 初始化
*/
int static initial(PLinkQueue p)
{
//第一个节点
PListNode new = applyNewNode();
//head指向第一个节点
p->head = new;
p->head->next = NULL;
p->rear = p->head;
p->length = 0;
}
/**
* 判断空
*/
int static checkEmpty(PLinkQueue p)
{
if(p->length == 0) return 1;
else return 0;
}
/**
* 进队列
*/
int static enQueue(PLinkQueue p)
{
int value;
printf("Input the enqueue value\n");
scanf("%d", &value);
//如果现在队列为空,说明插入的是第一个节点
if(checkEmpty(p)){
p->head->data = value;
}
else{
PListNode new = applyNewNode();
new->data = value;
p->rear->next = new;
p->rear = new;
}
p->length++;
}
/**
* 出队列
*/
int static deQueue(PLinkQueue p)
{
int value;
PListNode head;
if(checkEmpty(p)){
printf("The queue is empty\n");
exit(0);
}
head = p->head;
value = head->data;
p->head = head->next;
p->length--;
free(head);
printf("The dequeue value is %d\n", value);
}
/**
* 遍历
*/
int static travesal(LinkQueue queue)
{
PListNode index = queue.head;
if(checkEmpty(&queue)){
printf("Empty queue\n");
exit(0);
}
printf("----------------traversal-------------------\n");
for(int i = 0; i < queue.length; i++){
printf("%d\n", index->data);
index = index->next;
}
printf("----------------traversal-------------------\n");
}
int linkQueue()
{
LinkQueue p;
initial(&p);
enQueue(&p);
enQueue(&p);
enQueue(&p);
enQueue(&p);
deQueue(&p);
travesal(p);
}