#include <stdio.h>
#include <stdlib.h>
typedef int QElemType;
typedef struct Qnode {
QElemType data; //数据域
struct Qnode *next; //队列指针域
}QNode, *QueuePtr;
typedef struct {
QueuePtr front; //队列头指针
QueuePtr rear; //队列尾指针
}LinkQueue;
void InitQueue(LinkQueue &Q) { //构造队列
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if(!Q.front) {
printf("队列构造失败,程序退出!");
exit(!0);
}
Q.front -> next = NULL;
}
void DestroyQueue(LinkQueue &Q) { //销毁队列
while(Q.front) {
Q.rear = Q.front -> next;
free(Q.front);
Q.front = Q.rear;
}
printf("\n\n队列已销毁!\n\n");
}
void EnQueue(LinkQueue &Q,QElemType e) { //插入元素
QueuePtr p;
p = (QueuePtr)malloc(sizeof(QNode));
if(!p) {
printf("存储空间分配失败,程序退出!");
exit(!0);
}
printf("请输入进队列的值:");
scanf("%d",&e);
p -> data = e;
p -> next = NULL;
Q.rear -> next = p;
Q.r
C语言—链队列
最新推荐文章于 2024-03-11 22:33:08 发布