//链式队列定义及各类操作
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int ElemType;
typedef struct LinkNode
{
ElemType data;
struct LinkNode *next;
} LinkNode;
typedef struct LinkQueue
{
LinkNode *front, *rear;
} LinkQueue;
//初始化链式队列(带头节点)
void InitLinkQueue(LinkQueue Q){
//初始时,front,rear指针都指向头节点
Q.front = Q.rear = (LinkNode *)malloc(sizeof(LinkNode));
Q.front->next = NULL;
}
//判断链式队列式是否为空(带头节点)
void IsEmptyLinkQueue(LinkQueue Q){
if (Q.front == Q.rear)
return true;
else
return false;
}
//新元素e入链式队列(带头节点)
void EnLinkQueque(LinkQueue Q, ElemType e){
LinkNode *s = (LinkNode *)malloc(sizeof(LinkNode)); //申请内存空间
if (s == NULL)
return false; //内存空间申请失败
s->data = e; //元素e存入节点s
s->next = NULL; //新节点在队尾,尾指针指向NULL
Q.rear->next = s; //新节点s插入到为指针rear之后
Q.rear = s; //更新尾指针
}
//元素出链式队列(带头节点)
void DeLinkQueue(LinkQueue Q, ElemType e){
if (Q.front == Q.rear)
return false; //对空
LinkNode *temp = Q.front->next;
e = temp->data;
Q.front->next = temp->next;
if (Q.rear == temp)
Q.rear = Q.front;
free(temp);
return true;
}
11-07
413
08-14
243