Queue.h
#pragma once
#include"BinTree.h"
typedef PBTNode QDatatype;
typedef PBTNode DataType;
typedef struct Node
{
struct Node *_PNext;
DataType _data;
}Node, *PNode;
typedef struct Queue
{
PNode front;
PNode rear;
}Queue, *PQueue;
//初始化队列
void QueueInit(PQueue q);
//入队
void QueuePush(PQueue q, DataType data);
//出队
void QueuePop(PQueue q);
//创建新结点
PNode BuyNode(DataType data);
//判空
int QueueEmpty(PQueue q);
//查看队头元素
DataType QueueFrontData(PQueue q);
//查看队尾元素
DataType QueueRearData(PQueue q);
//查看队列长度
int QueueSize(PQueue q);
//销毁队列
void QueueDestory(PQueue q);
Queue.c
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"Queue.h"
//初始化队列
void QueueInit(PQueue q)
{
assert(q);
q->front = NULL;
q->rear = NULL;
}
void QueuePush(PQueue q, DataType data)
{
assert(q);
if (NULL == q->front)
{
q->front = q->rear = BuyNode(data);
}
else
{
q->rear->_PNext = BuyNode(data);
q->rear = q->rear->_PNext;
}
}
//出队
void QueuePop(PQueue q)
{
PNode pDel = NULL;
assert(q);
if (NULL == q->front)
{
return;
}
else
{
pDel = q->front;
q->front = q->front->_PNext;
free(pDel);
pDel = NULL;
}
}
//创建新结点
PNode BuyNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
if (NULL == pNewNode)
{
exit(EXIT_FAILURE);
}
pNewNode->_data = data;
pNewNode->_PNext = NULL;
return pNewNode;
}
//判空
int QueueEmpty(PQueue q)
{
assert(q);
if (NULL == q->front)
return 1;
return 0;
}
//查看队头元素
DataType QueueFrontData(PQueue q)
{
assert(q);
if (NULL == q->front)
exit(EXIT_FAILURE);
return q->front->_data;
}
//查看队尾元素
DataType QueueRearData(PQueue q)
{
assert(q);
if (NULL == q->front)
exit(EXIT_FAILURE);
return q->rear->_data;
}
//查看队列长度
int QueueSize(PQueue q)
{
int count = 0;
PNode pCur = NULL;
assert(q);
if (NULL == q->front)
{
return 0;
}
else
{
pCur = q->front;
while (pCur)
{
count++;
pCur = pCur->_PNext;
}
}
return count;
}
//销毁队列
void QueueDestory(PQueue q)
{
PNode pDel = NULL;
PNode pCur = NULL;
assert(q);
if (NULL == q->front)
{
return;
}
else
{
pCur = q->front;
q->front = NULL;
while (pCur)
{
pDel = pCur;
pCur = pCur->_PNext;
free(pDel);
}
}
}