queue.cpp
来自「掌握循环队列的基本操作及在顺序存储结构上的实现。数据结构」· C++ 代码 · 共 133 行
CPP
133 行
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "Queue.h"
void QueueInitial(LinkQueue pQ)
{
QueueNode *p;
Apply(p);
if (NULL==p)
{
printf("内存分配失败!");
exit(1);
}
pQ->front=p;
pQ->rear=pQ->front;
pQ->front->next=NULL;
}
int IsEmpty(LinkQueue pQ)
{
return pQ->front==pQ->rear;
}
void EnQueue(LinkQueue pQ,ElemType e)
{
QueueNode *p;
Apply(p);
if (NULL==p)
{
printf("内存分配失败!");
exit(1);
}
p->data=e;
p->next=NULL;
pQ->rear->next=p;
pQ->rear=p;
}
ElemType DeQueue(LinkQueue pQ)
{
QueueNode *first;
ElemType temp;
if (IsEmpty(pQ))
{
printf("空队!\n");
exit(1);
}
first=pQ->front->next;
temp=first->data;
pQ->front->next=first->next;
if (first->next==NULL)
{
pQ->rear=pQ->front;
}
free(first);
return temp;
}
ElemType GetFront(LinkQueue pQ)
{
QueueNode *first;
if (IsEmpty(pQ))
{
printf("空队!");
exit(1);
}
first=pQ->front->next;
return first->data;
}
void display(LinkQueue pQ)
{
QueueNode *first;
if (IsEmpty(pQ))
{
printf("空队!");
}
while (!IsEmpty(pQ))
{
printf("%-5c",GetFront(pQ));
first=pQ->front->next;
pQ->front->next=first->next;
if (first->next==NULL)
{
pQ->rear=pQ->front;
}
}
putchar('\n');
}
void MakeEmpty(LinkQueue pQ)
{
QueueNode *p,*q;
p=pQ->front->next;
while (p!=NULL)
{
q=p;
p=p->next;
free(q);
}
pQ->front->next=NULL;
pQ->rear=pQ->front;
}
void Destroy(LinkQueue pQ)
{
QueueNode *p,*q;
p=pQ->front;
if (p!=NULL)
{
q=p;
p=p->next;
free(q);
}
pQ->front=pQ->rear=NULL;
}
void Union(LinkQueue pQ1,LinkQueue pQ2)//将队列pQ2所指的队列中的元素加到pQ1所指的队列的队尾
{
QueueNode *first;
while (!IsEmpty(pQ2))
{
EnQueue(pQ1,GetFront(pQ2));
first=pQ2->front->next;
pQ2->front->next=first->next;
if (first->next==NULL)
{
pQ2->rear=pQ2->front;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?