强调内容
#include<stdio.h>
#include<assert.h>
#define QUEUESIZE 8
#define INC 2 //扩容增量为2
typedef int QueueElem
typedef struct Queue
{
QueueElem *data;
int front;
int tail;
int maxsize;//最大的
int cursize;//当前大小
}Queue;
typedef char ElemType ;
typedef struct BtNode // BinaryTreeNode
{
BtNode *leftchild;
BtNode *rightchild;
ElemType data;
}BtNode, *BinaryTree;
void Init_Queue(Queue *pe)//初始化队列
{
assert(pe!=NULL);
pe->front =0;
pe->tail =0;
pe->cursize =0;
pe->maxsize =QUEUESIZE;
pe->data =(QueueElem*)malloc(size(QueueElem)*pe->maxsize );
}
void Destry_Queue(Queue *pe)//销毁队列
{
assert(pe!=NULL);
free(pe->data );
pe->data =NULL;
pe->maxsize =0;
pe->front =0;
pe->tail =0;
pe->cursize =0;
}
bool empty(Queue *pe)//判断队列是否为空
{
assert(pe!=NULL);
return (Queue_size(pe)==0);
}
QueueElem *KR(QueueElem *p)//扩容
{
assert(p!=NULL);
return (QueueElem*)realloc(p,size(QueueElem)*INC );
}
bool is_full(Queue *pe)//判断队列是否满了
{
return (pe->cursize== pe->maxsize);
}
void Queue_push(Queue *pe,QueueElem x)//入队列
{
assert(pe!=NULL);//队列指针不为空
if(Queue_full(pe))//队列满的时候就扩容
{
pe->data =KR(pe->data);
pe->maxsize+=2;//队列的maxsize加2
}
pe->data [pe->tail ]=x;
pe->tail =(pe->tail +1)%pe->maxsize ;
}
bool front(QueueElem *pe)//top 对头(删除元素)
{
if (!empty(pe))
{
pe->front =(pe->front +1)%pe->maxsize;
pe->cursize--;//当前的使用的size减一
return 1;
}
return 0;
}
QueueElem pop(QueueElem *pe)//出队
{
return pe->data[pe->front];
}
void LevelOrder(BtNode *ptr)
{
if (ptr == NULL)这里写代码片
{
return;
}
Queue q;
init_queue(&q);
push_queue(&q,ptr); //将ptr的值赋予队列
while(!empty_queue(&q))
{
ptr = queue_front(&q); //将队列头的值给ptr
pop_queue(&q); //出队列
printf("%c ",ptr->data); //打印ptr的值
if (ptr->leftchild != NULL)
{
push_queue(&q,ptr->leftchild); //把左子树的值给队列
}
if (ptr->rightchild != NULL)
{
push_queue(&q,ptr->rightchild); //把右子树的值给队列
}
}
destroy_queue(&q); //摧毁二叉树
}