目录
1.头文件
include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MaxSize 50
2.初始化
typedef char ElemType;
typedef struct qnode{
ElemType data;
struct qnode *next;
}DataNode;
typedef struct {
DataNode *front;
DataNode *rear;
}LinkQuNode;
LinkQuNode *q;
q = (LinkQuNode * ) malloc(sizeof(LinkQuNode));
q -> front = q -> rear = NULL;
3.销毁队列
void DestroyQueue(LinkQuNode * s) {
DataNode *pre = s -> front,*p;
if(pre != NULL){
p = pre -> next;
while (p != NULL){
free(pre);
pre = p;
p = pre -> next;
}
free(pre);
}
free(s);
printf("销毁成功\n");
}
4.判空
bool EmptySQueue(LinkQuNode * s){
if(s -> rear == NULL){
printf("队列为空\n");
return false;
} else{
printf("队列不为空\n");
return true;
}
}
5.入队
bool enQueue(LinkQuNode *s,ElemType e){
DataNode *p;
p = (DataNode*) malloc(sizeof(DataNode));
p -> data = e;
if(s -> rear == NULL){
s -> front = s -> rear = p;
} else{
s -> rear -> next = p;
s -> rear = p;
}
printf("入队成功\n");
return true;
}
6.出队
bool dlQueue(LinkQuNode *s,ElemType e){
DataNode *t;
if(s -> rear == NULL){
printf("队列为空\n");
return false;
}
t = s -> front;
if(s -> front == s -> rear)
s -> front = s -> rear = NULL;
else
s -> front = s -> front -> next;
e = t -> data;
free(t);
printf("%c出队成功\n",e);
return true;
}