[文件] DynaLnkQueue.cpp ~ 5KB 下载(215)
001 | /*** |
002 | *DynaLnkQueue.cpp - 动态链式队列,即队列的动态链式存储实现 |
003 | * |
004 | * |
005 | *题目:实验3-2 队列的动态链式存储实现 |
006 | * |
007 | ****/ |
008 |
009 | #include <stdlib.h> |
010 | #include <malloc.h> |
011 | #include <memory.h> |
012 | #include <assert.h> |
013 | #include "DynaLnkQueue.h" |
014 | #define NULL 0 |
015 |
016 | /*------------------------------------------------------------ |
017 | 操作目的: 初始化队列 |
018 | 初始条件: 无 |
019 | 操作结果: 构造一个空的队列 |
020 | 函数参数: |
021 | LinkQueue *Q 待初始化的队列 |
022 | 返回值: |
023 | bool 操作是否成功 |
024 | ------------------------------------------------------------*/ |
025 | bool InitQueue(LinkQueue *Q) |
026 | { |
027 | Q ->front = Q ->rear = (QueuePtr) malloc ( sizeof (QNode)); |
028 | if (Q ->front == NULL) |
029 | return false ; |
030 | Q ->front ->next = NULL; |
031 | return true ; |
032 | } |
033 |
034 | /*------------------------------------------------------------ |
035 | 操作目的: 销毁队列 |
036 | 初始条件: 队列Q已存在 |
037 | 操作结果: 销毁队列Q |
038 | 函数参数: |
039 | LinkQueue *Q 待销毁的队列 |
040 | 返回值: |
041 | 无 |
042 | ------------------------------------------------------------*/ |
043 | void DestroyQueue(LinkQueue *Q) |
044 | { |
045 | assert (Q != NULL); |
046 | while (Q ->front) |
047 | { |
048 | Q ->rear = Q ->front ->next; |
049 | free (Q ->front); |
050 | Q ->front = Q ->rear; |
051 | } |
052 | } |
053 |
054 | /*------------------------------------------------------------ |
055 | 操作目的: 判断队列是否为空 |
056 | 初始条件: 队列Q已存在 |
057 | 操作结果: 若Q为空队列,则返回true,否则返回false |
058 | 函数参数: |
059 | LinkQueue Q 待判断的队列 |
060 | 返回值: |
061 | bool 是否为空 |
062 | ------------------------------------------------------------*/ |
063 | bool QueueEmpty(LinkQueue Q) |
064 | { |
065 | assert (Q.front != NULL && Q.rear != NULL); |
066 | if (Q.front == Q.rear) |
067 | return true ; |
068 | else |
069 | return false ; |
070 | } |
071 | /*------------------------------------------------------------ |
072 | 操作目的: 得到队列的长度 |
073 | 初始条件: 队列Q已存在 |
074 | 操作结果: 返回Q中数据元素的个数 |
075 | 函数参数: |
076 | LinkQueue Q 队列Q |
077 | 返回值: |
078 | int 数据元素的个数 |
079 | ------------------------------------------------------------*/ |
080 | int QueueLength(LinkQueue Q) |
081 | { |
082 | assert (Q.front != NULL); |
083 | QueuePtr p = Q.front; |
084 | int Length = 0; |
085 | while (p != Q.rear) |
086 | { |
087 | Length++; |
088 | p = p->next; |
089 | } |
090 | return Length; |
091 | } |
092 | /*------------------------------------------------------------ |
093 | 操作目的: 得到队列首元素 |
094 | 初始条件: 队列Q已存在 |
095 | 操作结果: 用e返回队列首元素 |
096 | 函数参数: |
097 | LinkQueue Q 队列Q |
098 | ElemType *e 队列首元素的值 |
099 | 返回值: |
100 | bool 操作是否成功 |
101 | ------------------------------------------------------------*/ |
102 | bool GetHead(LinkQueue Q, ElemType *e) |
103 | { |
104 | assert (Q.front != NULL); |
105 | if (QueueEmpty(Q)) |
106 | return false ; |
107 | else |
108 | { |
109 | *e = Q.front ->next ->data; |
110 | return true ; |
111 | } |
112 | |
113 | } |
114 | /*------------------------------------------------------------ |
115 | 操作目的: 遍历队列 |
116 | 初始条件: 队列Q已存在 |
117 | 操作结果: 依次对Q的每个元素调用函数fp |
118 | 函数参数: |
119 | LinkQueue Q 队列Q |
120 | void (*fp)() 访问每个数据元素的函数指针 |
121 | 返回值: |
122 | 无 |
123 | ------------------------------------------------------------*/ |
124 | void QueueTraverse(LinkQueue Q, void (*fp)(ElemType)) |
125 | { |
126 | assert (Q.front != NULL); |
127 | QueuePtr p = Q.front ->next; |
128 | while (p) |
129 | { |
130 | (*fp)(p ->data); |
131 | p = p ->next; |
132 | } |
133 | } |
134 |
135 | /*------------------------------------------------------------ |
136 | 操作目的: 清空队列 |
137 | 初始条件: 队列Q已存在 |
138 | 操作结果: 将队列清空 |
139 | 函数参数: |
140 | LinkQueue *Q 队列Q |
141 | 返回值: |
142 | 无 |
143 | ------------------------------------------------------------*/ |
144 | void ClearQueue(LinkQueue *Q) |
145 | { |
146 | assert (Q ->front != NULL); |
147 | QueuePtr p = Q ->front ->next; |
148 | while (p) |
149 | { |
150 | Q ->front ->next = p ->next; |
151 | free (p); |
152 | p = Q ->front ->next; |
153 | } |
154 | } |
155 |
156 | /*------------------------------------------------------------ |
157 | 操作目的: 在队列末尾插入元素e |
158 | 初始条件: 队列Q已存在 |
159 | 操作结果: 插入元素e作为队列新的尾结点 |
160 | 函数参数: |
161 | LinkQueue *Q 队列Q |
162 | ElemType e 待插入的数据元素 |
163 | 返回值: |
164 | bool 操作是否成功 |
165 | ------------------------------------------------------------*/ |
166 | bool EnQueue(LinkQueue *Q, ElemType e) |
167 | { |
168 | QueuePtr temp = (QueuePtr ) malloc ( sizeof (QNode)); |
169 | if (!temp) |
170 | return false ; |
171 | temp ->data = e; |
172 | temp ->next = NULL; |
173 | Q->rear ->next = temp; |
174 | Q ->rear = temp; |
175 | return true ; |
176 | } |
177 |
178 | /*------------------------------------------------------------ |
179 | 操作目的: 删除链式队列的头结点 |
180 | 初始条件: 队列Q已存在 |
181 | 操作结果: 删除链式队列的头结点 |
182 | 函数参数: |
183 | LinkQueue *Q 队列Q |
184 | ElemType *e 待插入的数据元素 |
185 | 返回值: |
186 | bool 操作是否成功 |
187 | ------------------------------------------------------------*/ |
188 | bool DeQueue(LinkQueue *Q, ElemType *e) |
189 | { |
190 | if (Q ->front == Q->rear) |
191 | return false ; |
192 | QueuePtr temp = Q->front ->next; |
193 | *e = temp ->data; |
194 | Q ->front ->next= temp ->next; |
195 | if (Q ->rear == temp) |
196 | Q ->rear = Q ->front; |
197 | free (temp); |
198 | return true ; |
199 | } |
[文件] DynaLnkQueue.h ~ 1KB 下载(139)
01 | /*** |
02 | *DynaLnkQueue.h - 动态链式队列的定义 |
03 | * |
04 | ****/ |
05 |
06 | #if !defined(DYNALNKQUEUE_H) |
07 | #define DYNALNKQUEUE_H |
08 |
09 | #include "ElemType.h" |
10 |
11 | /*------------------------------------------------------------ |
12 | // 链式队列结构的定义 |
13 | ------------------------------------------------------------*/ |
14 |
15 | typedef struct Node |
16 | { |
17 | ElemType data; // 元素数据 |
18 | struct Node *next; // 链式队列中结点元素的指针 |
19 | } QNode, *QueuePtr; |
20 |
21 | typedef struct |
22 | { |
23 | QueuePtr front; // 队列头指针 |
24 | QueuePtr rear; // 队列尾指针 |
25 | } LinkQueue; |
26 |
27 | /*------------------------------------------------------------ |
28 | // 链式队列的基本操作 |
29 | ------------------------------------------------------------*/ |
30 |
31 | bool InitQueue(LinkQueue *Q); |
32 | void DestroyQueue(LinkQueue *Q); |
33 | bool QueueEmpty(LinkQueue Q); |
34 | int QueueLength(LinkQueue Q); |
35 | bool GetHead(LinkQueue Q, ElemType *e); |
36 | void QueueTraverse(LinkQueue Q, void (*fp)(ElemType)); |
37 | void ClearQueue(LinkQueue *Q); |
38 | bool EnQueue(LinkQueue *Q, ElemType e); |
39 | bool DeQueue(LinkQueue *Q, ElemType *e); |
40 |
41 | #endif /* DYNALNKQUEUE_H */ |
[文件] ElemType.cpp ~ 208B 下载(102)
01 | /*** |
02 | *ElemType.cpp - ElemType的实现 |
03 | * |
04 | ****/ |
05 |
06 | #include <stdio.h> |
07 | #include "ElemType.h" |
08 |
09 | int compare(ElemType x, ElemType y) |
10 | { |
11 | return (x-y); |
12 | } |
13 |
14 | void visit(ElemType e) |
15 | { |
16 | printf ( "%d\n" , e); |
17 | } |
[文件] ElemType.h ~ 207B 下载(106)
01 | /*** |
02 | *ElemType.h - ElemType的定义 |
03 | * |
04 | ****/ |
05 |
06 | #ifndef ELEMTYPE_H |
07 | #define ELEMTYPE_H |
08 |
09 | typedef int ElemType; |
10 |
11 | int compare(ElemType x, ElemType y); |
12 | void visit(ElemType e); |
13 |
14 | #endif /* ELEMTYPE_H */ |
[文件] Lab.cpp ~ 250B 下载(98)
01 | #include <stdio.h> |
02 | #include <stdlib.h> |
03 | #include "DynaLnkQueue.h" |
04 |
05 | int main() |
06 | { |
07 | // TODO: Place your test code here |
08 | /*LinkQueue W; |
09 | InitQueue(&W); |
10 | EnQueue(&W,1); |
11 | EnQueue(&W,2); |
12 | QueueTraverse(W,visit);*/ |
13 | system ( "PAUSE" ); |
14 | return 0; |
15 | } |