循环队列、链式队列的基本操作

1.循环队列

输入

为循环队列的操作系列,每个操作一行,具体见样例。

输出

如果输入为"EnQueue e", 队已满输出"EnQueue failure",否则将e入队后再输出从队头到队尾的所有元素。
如果输入为"DeQueue", 队为空输出"DeQueue failure",否则删除队头元素后再输出队列中的所有元素。
如果输入为"Empty",则输出队列是否为空。
如果输入为"Length",则输出队列的长度。
如果输入为"GetHead",队列为空则输出"GetHead failure",否则输出队列的头元素输出从队头到队尾的所有元素。
具体见样例。

样例输入 Copy

Empty
Length
Head
DeQueue
EnQueue 1
EnQueue 2
EnQueue 3
Empty
Head
DeQueue
EnQueue 4
EnQueue 5
EnQueue 6
Length
EnQueue 7
DeQueue

样例输出 Copy

Queue is empty
The queue length is 0
GetHead failure
DeQueue failure
1
1 2
1 2 3
Queue is not empty
The front element is 1
2 3
2 3 4
2 3 4 5
2 3 4 5 6
The queue length is 5
EnQueue failure
3 4 5 6

代码

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;

#define MAXQSIZE 6
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;

typedef struct {
   
   QElemType *base;//初始化时动态分配存储空间
   int front;//头指针
   int rear;//尾指针
} SqQueue;

//算法3.11 循环队列的初始化
Status InitQueue(SqQueue &q)   //构造一个空队列Q
{
   
   q.base = new QElemType[MAXQSIZE]; //为队列分配一个最大容量为MAXSIZE的数组空间
   if(!q.base)
      exit(OVERFLOW); //存储分配失败
   q.front = q.rear = 0; //头指针和尾指针置为零,队列为空
   return OK;
}

void DestroyQueue(SqQueue &q)
{
   
   /* 销毁队列Q,Q不再存在 */
   if(q.base)
      delete []q.base;
   q.base = NULL;
   q.front = q.rear = 0;
}

//算法3.12 求循环队列的长度
int QueueLength(SqQueue q)   //返回Q的元素个数,即队列的长度
{
   
	return (q.rear -q.front +MAXQSIZE)%MAXQSIZE;
}

bool QueueEmpty(SqQueue q)
{
   
	return (q.front ==q.rear ); 
}
//算法3.13 循环队列的入队
Status EnQueue(SqQueue &q, QElemType e)   //插入元素e为Q的新的队尾元素
{
   
	if((q.rear +1)%MAXQSIZE==q.front )
		return ERROR
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shuo..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值