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));
q->rear = q->front;
q->front->next = NULL;
}
//销毁带头结点的链队 //头指针指向空
bool DestroyQueue(LinkQuNode &q)
{
DataNode *p,*pre;
pre = q->front;
p = pre->next;
while(pre!=NULL)
{
free(pre);
pre = p;
p = p->next;
}
free(q);
}
//到头结点的链队判空
bool QueueEmpty(LinkQuNode &q)
{
return(q->rear == q->front);
}
//带头结点的入队操作 从队尾入
bool Enqueue(LinkQuNode &q,ElemType e)
{
*s = (DataNode *)malloc(sizeof(DataNode));
s->next = NULL;
s->data = e;
q->rear->next = s;
q->rear = s;
return true;
}
//带头结点的出队操作 //从对头后出
bool Dequeue(LinkQuNode &q,ElemType e)
{
if(q->front->next ==NULL)//(p->front == p->rear)效果一样
return false;
DataNode *t;
t= p->front ->next;
e = t->data;
p->front->next = t->next;
free(t);
return true;
}
学习c++带头结点的链队基本操作(王道数据结构队列的链式存储)
于 2022-04-29 10:03:33 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)