用前面已经实现的线性表来实现一个整数队列(队列里的数据是整数)。共需要补全三个函数(也是队列的基本功能):
判断队列空queueEmpty、入列enQueue和出列deQueue。
相关知识
队列是一种功能受限的线性表,具有先进先出的特性。队列仅允许从一头出列(这一头称为队列头),从另外一头入列(队列尾)。
入列操作是将结点插入到队列尾(该结点称为新的队列尾);
出列操作是从队列头删除头结点,并获取删除节点的数据(原头结点的后续结点为新的头结点)。
接下来首先声明结构类型:
// 定义结点结构
struct node
{
int data; // 数据域
node * next; // 指针域,指向下一个结点
};
typedef node * intQueue; // 定义类型别名,intQueue即相当于node*
其中:定义intQueue是为了使程序的可读性更好一些。因为本关是用线性表实现队列,而访问一个线性表其实只需要一个链表头指针就可以了,intQueue实际上就是node类型,所以后面可以这样声明队列iq:
intQueue iq=NULL; // 声明队列iq,并初始化为空队列(空线性表)
实际上iq就是一个node类型的指针,用它可以访问一个线性表,也就可以看成一个队列。
三个需要用户补全的函数的函数原型分别为:
- 判断队列是否为空:判断队列iq是否为空,为空则返回true,否则返回false;
bool queueEmpty(intQueue iq); - 入列:实现将整数num入列到队列iq。没有返回值,因为采用链表实现队列,只要还有内存,入列总会成功;
void enQueue(intQueue &iq, int num); - 出列:从队列iq出列,如果队列非空,则返回出列的数据,否则返回-1。
int deQueue(intQueue &iq);
温馨提示:可以使用线性表已有的功能来实现这些函数。
#include <iostream>
#include "linearList.h"
#include "mqueue.h"
using namespace std;
typedef node * intQueue; // 定义类型别名,intQueue即相当于node*
// 函数queueEmpty:判断队列iq是否为空
// 参数:iq-整数队列
// 返回值:true-队列iq为空,false-iq不为空
bool queueEmpty(intQueue iq);
// 函数enQueue:将整数num入列到iq
// 参数:iq-整数队列,传引用,入列有可能改变队列头指针,num-入列的整数
// 返回值:无,只要还有内存,入列总会成功
void enQueue(intQueue &iq, int num);
// 函数deQueue:出列
// 参数:iq-整数队列,传引用,出列有可能改变队列头指针
// 返回值:出列结点的数据,如果队列为空,返回-1
int deQueue(intQueue &iq);
//定义结点结构
struct node
{
int data; //数据域
node * next; //指针域,指向下一个结点
};
//函数listLength:计算并返回链表的长度
//参数:h-链表头指针
//返回值:链表长度
int listLength(node * h);
//函数delHas:删除链表中data为n的结点,如果有多个这样的结点,只删除第一个
//参数:h-链表头指针,n-结点包含的数据
//返回值:删除结束后链表首结点地址
node * delHas(node * h, int n);
//函数delAt:删除链表中序号为i的结点,如果i是非法序号则不做操作
//参数:h-链表头指针,i-要删除结点的序号
//返回值:删除结束后链表首结点地址
node * delAt(node * h, int i);
//函数search:在链表中查找包含数据num的结点
//参数:h-链表头指针,num-要查找的数据
//返回值:找到了返回该结点的地址,否则返回NULL
node * search(node * h, int num);
//函数insertSort:链表排序插入
//参数:h-链表头指针,t-指向要插入的结点
//返回值:插入结点后链表的首结点地址
node * insertSort(node *h, node *t);
//函数insertHead:链表头部插入
//参数:h-链表头指针,t-指向要插入的结点
//返回值:插入结点后链表的首结点地址
node * insertHead(node *h, node *t);
//函数printList:输出链表,每个数据之间用一个空格隔开
//参数:h-链表头指针
void printList(node *h);
//函数insertTail:链表尾部插入
//参数:h-链表头指针,t-指向要插入的结点
//返回值:插入结点后链表的首结点地址
node *insertTail(node *h, node *t);
//函数delList:删除链表,释放空间
//参数:h-链表头指针
void delList(node *h);
//函数delList:删除链表,释放空间
//参数:h-链表头指针
void delList(node *h)
{
node *p=h; //指针p指向头结点,第一个要删除的结点
while(p) //这个结点是存在的
{
h = h->next; //头指针h指向下一个结点(下一个结点的地址存在当前结点的指针域中,即h->next中
delete p; //删除p指向的结点
p = h; //p指向当前的头结点,即下一个要删除的结点
}
}
//函数printList:输出链表,每个数据之间用一个空格隔开
//参数:h-链表头指针
void printList(node *h)
{
cout<<"List:";
while(h)
{//h为真,即h指向的结点存在,则输出该结点的数据
cout<<" "<<h->data; //输出结点数据
h=h->next; //将该结点的指针域赋值给h,h就指向了下一个结点
}
cout<<endl; //输出换行符
}
//函数insertTail:链表尾部插入
//参数:h-链表头指针,t-指向要插入的结点
//返回值:插入结点后链表的首结点地址
node *insertTail(node *h, node *t)
{
if(h==NULL) //空链表单独处理
{
t->next=NULL; //链表尾指针置为NULL
return t; //返回第一个结点的地址(即链表头指针)
}
//非空链表的情况
node *p=h;
//让p指向最后一个结点
while(p->next)
p=p->next;
p->next = t; //让最后一个结点的指针域指向结点t
t->next=NULL; //链表尾指针置为NULL
return h; //返回第一个结点的地址(即链表头指针)
}
//函数insertHead:链表头部插入
//参数:h-链表头指针,t-指向要插入的结点
//返回值:插入结点后链表的首结点地址
node * insertHead(node *h, node *t)
{
t->next=h;
return t;
}
//函数insertSort:链表排序插入
//参数:h-链表头指针,t-指向要插入的结点
//返回值:插入结点后链表的首结点地址
node * insertSort(node *h, node *t)
{
node *p=NULL,*q=h; //定位第一个插入点:链首
while(q && q->data<t->data) //查找插入点
{//两个指针并行后移
p=q;
q=q->next;
}
if(p==NULL) //插入链首
{
t->next = h;
return t;
}
if(q==NULL) //插入链尾
{
p->next = t;
t->next = NULL;
return h;
}
//插入p、q之间
t->next=q;
p->next=t;
return h;
}
//函数search:在链表中查找包含数据num的结点
//参数:h-链表头指针,num-要查找的数据
//返回值:找到了返回该结点的地址,否则返回NULL
node * search(node * h, int num)
{
while(h)
{//h为真,即h指向的结点存在
if(h->data==num)
return h;
h=h->next; //将该结点的指针域赋值给h,h就指向了下一个结点
}
return NULL; //没找到包含num的结点
}
//函数delAt:删除链表中序号为i的结点,如果i是非法序号则不做操作
//参数:h-链表头指针,i-要删除结点的序号
//返回值:删除结束后链表首结点地址
node * delAt(node * h, int i)
{
if(i<0) //序号非法,不删除
return h;
node *p=NULL, *q = h; // 定位删除结点,试图让q指向要删除结点,p指向其前面的结点
for(int k=0;k<i;k++)
{
if(q->next==NULL) //后面没有结点了,序号非法
return h;
p=q;
q=q->next;
}
if(p) //p指向的结点存在,不是删除首结点
{
//删除q指向的结点,让p指向结点的指针域指向q的后续结点
p->next = q->next;
//释放空间
delete q;
return h;
}
else //删除首结点
{
h = q->next; //下一个结点成了首结点
//释放空间
delete q;
return h;
}
}
//函数delHas:删除链表中data为n的结点,如果有多个这样的结点,只删除第一个
//参数:h-链表头指针,n-结点包含的数据
//返回值:删除结束后链表首结点地址
node * delHas(node * h, int n)
{
node *p=NULL, *q=h; //p为要删除结点的前结点,q指向要删除结点
while(q)
{//h为真,即h指向的结点存在
if(q->data==n)
break; //找到了
if(q->next==NULL)//后面没有结点了,没有结点满足条件
return h; //不删除,直接返回
//继续往后找,两个指针一起后移
p=q;
q=q->next;
}
//删除q指向的结点
if(p==NULL)//删除头结点
{
h=q->next;//下一个结点变成头结点
delete q;//删除结点
return h;
}
//不是头结点
p->next=q->next;//把q指向结点的指针域(q后面结点的地址)赋值给p指向结点的指针域
return h;
}
//函数listLength:计算并返回链表的长度
//参数:h-链表头指针
//返回值:链表长度
int listLength(node * h)
{
int n=0;
while(h)
{
n++;
h=h->next;
}
return n;
}
int main()
{
intQueue iq=NULL; //声明队列iq,并初始化为空队列(空线性表)
int command; //输入指令:1-入列,0-出列,-1-退出,其它-非法操作
int n;
//输入指令
cin>>command;
while(command!=-1)
{
switch(command)
{
case 0:
if(!queueEmpty(iq))
{//队列非空
n = deQueue(iq); //出列
cout<<n<<endl; //输出出列的值
}
else
cout<<"Queue empty"<<endl;
break;
case 1:
cin>>n; //输入要入列的数据
enQueue(iq,n);
cout<<"enqueue "<<n<<endl;
break;
default:;
}
cin>>command;//输入下一条指令
}
//删除队列
delList(iq);
return 0;
}
// 函数queueEmpty:判断队列iq是否为空
// 参数:iq-整数队列
// 返回值:true-队列iq为空,false-iq不为空
bool queueEmpty(intQueue iq)
{
return (iq==NULL); // iq为NULL,则队列为空
}
// 函数enQueue:将整数num入列到iq
// 参数:iq-整数队列,传引用,入列有可能改变队列头指针,num-入列的整数
// 返回值:无,只要还有内存,入列总会成功
void enQueue(intQueue &iq, int num)
{
// 准备结点
node *t=new node;
t->data=num;
t->next=NULL;
// 结点插入到尾部
iq = insertTail(iq,t);
}
// 函数deQueue:出列
// 参数:iq-整数队列,传引用,出列有可能改变队列头指针
// 返回值:出列结点的数据,如果队列为空,返回-1
int deQueue(intQueue &iq)
{
if(queueEmpty(iq))
return -1;
// 获取队列头结点的数据
int n = iq->data;
// 删除队列头结点(出列)
iq = delAt(iq,0);
// 返回出列数据
return n;
}