我是靠谱客的博主 单纯仙人掌,这篇文章主要介绍【数据结构初学笔记04】队列的存储实现,现在分享给大家,希望可以做个参考。

01队列的顺序存储实现

包括顺序存储实现,入队列,出队列(队头front,队尾rear)

复制代码
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
//01.存储实现 #include <stdio.h> #include <stdlib.h> #define ElementType int #define MaxSize 10 typedef struct QNode *Queue; struct QNode { ElementType Data[MaxSize]; int rear;//记录尾元素序号 int front;//记录头元素序号 }; int main(void) { struct QNode Q; } //02.入队列,队列采用循环结构可以最大限度利用空间 void AddQ(Queue PtrQ,ElementType item) { if((PtrQ->rear+1)%MaxSize==PtrQ->front) { printf("队列满"); return; } PtrQ->rear = (PtrQ->rear+1)%MaxSize; PtrQ->Data[PtrQ->rear] = item; } //03.出队列 ElementType DeleteQ(Queue PtrQ) { ElementType item; if(PtrQ->front==PtrQ->rear) { printf("队列空"); return 0; } PtrQ->front = (PtrQ->front+1)%MaxSize; return (PtrQ->Data[PtrQ->front]); }

02队列的链式存储实现

包括存储实现(使用链队列结构指示队头和队尾)、入队、出队(不带头节点的链表队列)

复制代码
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
//01.存储实现 #include <stdio.h> #include <stdlib.h> #define ElementType int typedef struct QNode *Queue; struct Node { ElementType Data; struct Node *Next; }; /*这里采用链队列结构,用于指示队头和队尾。*/ struct QNode { struct Node *rear;//指向队尾的节点 struct Node *front;//指向队头的节点 }; int main(void) { Queue PtrQ; return 0; } //02.入队操作 void AddQ(Queue PtrQ,ElementType item) { struct Node *TempCell; TempCell = (struct Node*)malloc(sizeof(struct Node)); TempCell->Data = item; PtrQ->rear->Next = TempCell; PtrQ->rear = TempCell; } //03.出队操作,不带头节点的链表队列 ElementType DeleteQ(Queue PtrQ) { struct Node *TempCell; ElementType item; if(PtrQ->front==NULL) { printf("队列空"); return 0; } TempCell = PtrQ->front; if(PtrQ->front==PtrQ->rear) { printf("队列只有一个元素"); PtrQ->front = NULL; PtrQ->rear = NULL; } PtrQ->front = TempCell->Next; item = TempCell->Data; free(TempCell); return item; }

结束

课程来源:浙江大学数据结构慕课MOOC

最后

以上就是单纯仙人掌最近收集整理的关于【数据结构初学笔记04】队列的存储实现的全部内容,更多相关【数据结构初学笔记04】队列内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(77)

评论列表共有 0 条评论

立即
投稿
返回
顶部