/*BiTree_Queue.h 操作二叉树使用的队列*/
//构造一个空队列
Status InitQueue(LinkQueue &L)
{
L.rear=L.front=(QueueLink)malloc(sizeof(QNode));
if(!L.rear)exit(-1);
L.front->next=NULL;
return TRUE;
}
//销毁队列
Status DestroyQueue(LinkQueue &L)
{
while(L.front)
{
L.rear=L.front->next;
L.front=L.rear->next;
free(L.rear);
// L.front=L.rear;
}
return TRUE;
}
//清空队列
Status ClearQueue(LinkQueue &L)
{
QueueLink q,p=L.front->next;
while(p)
{
q=p;
p=p->next;
free(q);
}
L.rear=L.front;
return TRUE;
}
//进队列
Status EnQueue(LinkQueue &L,ElemType e)
{
QueueLink p;
p=(QueueLink)malloc(sizeof(QNode));
if(!p)exit(-1);
p->data=e;
p->next=NULL;
L.rear->next=p;
L.rear=p;
return TRUE;
}
//出队列
Status DeQueue(LinkQueue &L,ElemType &e)
{
if(L.front==L.rear)return FALSE;
QueueLink p=L.front->next;
e=p->data;
L.front->next=p->next;
if(L.rear==p)L.rear=L.front;
free(p);
return TRUE;
}
//判断队列是否为空
Status QueueEmpty(LinkQueue L)
{
if(L.rear==L.front)
return TRUE;
return FALSE;
}
//得到ElemType &e队首元素
Status GetHead(LinkQueue L,ElemType &e)
{
if(QueueEmpty(L))return FALSE;
e=L.front->next->data;
return TRUE;
}
//返回队列长度
int QueueLength(LinkQueue L)
{
int i=0;
QueueLink p=L.front->next;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}
//构造一个空队列
Status InitQueue(LinkQueue &L)
{
L.rear=L.front=(QueueLink)malloc(sizeof(QNode));
if(!L.rear)exit(-1);
L.front->next=NULL;
return TRUE;
}
//销毁队列
Status DestroyQueue(LinkQueue &L)
{
while(L.front)
{
L.rear=L.front->next;
L.front=L.rear->next;
free(L.rear);
// L.front=L.rear;
}
return TRUE;
}
//清空队列
Status ClearQueue(LinkQueue &L)
{
QueueLink q,p=L.front->next;
while(p)
{
q=p;
p=p->next;
free(q);
}
L.rear=L.front;
return TRUE;
}
//进队列
Status EnQueue(LinkQueue &L,ElemType e)
{
QueueLink p;
p=(QueueLink)malloc(sizeof(QNode));
if(!p)exit(-1);
p->data=e;
p->next=NULL;
L.rear->next=p;
L.rear=p;
return TRUE;
}
//出队列
Status DeQueue(LinkQueue &L,ElemType &e)
{
if(L.front==L.rear)return FALSE;
QueueLink p=L.front->next;
e=p->data;
L.front->next=p->next;
if(L.rear==p)L.rear=L.front;
free(p);
return TRUE;
}
//判断队列是否为空
Status QueueEmpty(LinkQueue L)
{
if(L.rear==L.front)
return TRUE;
return FALSE;
}
//得到ElemType &e队首元素
Status GetHead(LinkQueue L,ElemType &e)
{
if(QueueEmpty(L))return FALSE;
e=L.front->next->data;
return TRUE;
}
//返回队列长度
int QueueLength(LinkQueue L)
{
int i=0;
QueueLink p=L.front->next;
while(p!=NULL)
{
p=p->next;
i++;
}
return i;
}