修改# include < malloc . h > typedef char ElemType ; typedef struct DataNode ElemType data ; struct DataNode * next , ] DataNode , typedef struct //链队数据结点类型 DataNode * front , DataNode * rear ;] LinkQuNode ; void InitQueue ( LinkQuNode * q ) //链队类型 q =( LinkQuNode *) malloc ( sizeof ( LinkQuNode )); g -> front = q -> rear = NULL ; void DestroyQueue ( LinkQuNode * q ) DataNode * p = q -> front .* x :// p 指向队头数捉结占节( p != NULL ) /経放数据结点占用空间 r = p -/ next ; while ( r != NULL ) free ( p ); p = r , r = p -> next , free ( p ) free ( q ) //释放链队结点占用空间 bool QueueEmpty ( LinkQuNode * q ) return ( q -) rear == NULL ); void enQueue ( LinkQuNode * q , ElemType e ) anaNode *) malloc ( sizeof ( DataNode )); p -> data = e , p -> next = NULL ; if ( q -/ rear == MULL ) else //若链队为空,则新结点是队首结点又是队尾结点//将 p 结点链到队尾,并将 rear 指向它 q -) front = q -) rear = p ; q -> rear -> next = p ; q -> rear = p ; bool deQueue ( LinkQuNode * q ElenType & e ) rear == NULL ) return false t = a 。 front 人列中只有一个结望问第一个数据结点 //队列中有多个结点时 //队列为空 else
时间: 2023-12-10 12:37:56 浏览: 128
#include <malloc.h>
修改后的代码如下:
```
#include <stdlib.h>
#include <stdbool.h>
typedef char ElemType;
typedef struct DataNode {
ElemType data;
struct DataNode *next;
} DataNode;
typedef struct {
DataNode *front, *rear;
} LinkQuNode;
void InitQueue(LinkQuNode *q) {
q = (LinkQuNode *) malloc(sizeof(LinkQuNode));
q->front = q->rear = NULL;
}
void DestroyQueue(LinkQuNode *q) {
DataNode *p = q->front;
DataNode *r;
while (p != NULL) {
r = p->next;
free(p);
p = r;
}
free(q);
}
bool QueueEmpty(LinkQuNode *q) {
return (q->rear == NULL);
}
void enQueue(LinkQuNode *q, ElemType e) {
DataNode *p = (DataNode *) malloc(sizeof(DataNode));
p->data = e;
p->next = NULL;
if (q->rear == NULL) { // 若链队为空,则新结点是队首结点又是队尾结点
q->front = q->rear = p;
} else {
q->rear->next = p; // 将 p 结点链到队尾,并将 rear 指向它
q->rear = p;
}
}
bool deQueue(LinkQuNode *q, ElemType &e) {
if (q->rear == NULL) { // 队列为空
return false;
} else {
DataNode *t = q->front;
e = t->data;
if (q->front == q->rear) { // 队列中只有一个结点,第一个数据结点
q->front = q->rear = NULL;
} else { // 队列中有多个结点时
q->front = t->next;
}
free(t);
return true;
}
}
```
阅读全文