7-2 实验2-2(循环队列)
7-2 实验2-2(循环队列)
用数组的方式实现循环队列,包括CreateQueue()、IsEmpty()、IsFull()、AddQ()、DeleteQ()等基本操作。数组的长度设置为5,并且要求队列中最多可容纳5个元素。
输入格式:
输入第1行是一个整数n,表示之后还有n行输入。
每行输入表示对队列的一条操作指令,格式是“指令编号 参数(如有)”。
指令编号为1,表示AddQ操作,此时参数为进入队列的元素值。
指令编号为2,表示DeleteQ操作,此时没有参数,从队列中取出元素并输入这个元素的值。如果队列为空,则输出-1。
输出格式:
从队列中取出元素时,输出这个元素的值,并在后面加一个空格。如果队列为空,则输出-1。
n条指令执行完后,在新的一行输出队列中剩余元素的个数,之后换行。
按从队首至队尾的顺序输出队列中的剩余元素,之间用一个空格隔开。如果队列为空,则不用输出。
输入样例:
在这里给出一组输入。例如:
10
1 10
1 20
1 30
1 40
1 50
2
2
1 60
2
1 70
输出样例:
在这里给出相应的输出。例如:
10 20 30
4
40 50 60 70
代码长度限制 16 KB
时间限制 400 ms
内存限制 64 MB
栈限制 8192 KB
C++代码
由题要求顺序储存,代码如下:
#include <iostream>
#define ERROR -1;
using namespace std;
// 7-2 实验2-2(循环队列)typedef int ElementType;
typedef int Position;
typedef struct QNode * PtrToQNode;
struct QNode {ElementType * Data;Position Front, Rear;int MaxSize;
};
typedef PtrToQNode Queue;Queue CreateQueue(int MaxSize) {Queue Q = (Queue)malloc(sizeof(struct QNode));Q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType));Q->Front = Q->Rear = 0;Q->MaxSize = MaxSize; // P85采用牺牲一个存储空间实现区分队列空与队列满,故实际只能存MaxSize-1个元素return Q;
}bool IsFull(Queue Q) {// 判断队列满return (Q->Rear+1) % Q->MaxSize == Q->Front;
}bool AddQ(Queue Q, ElementType X) {if(IsFull(Q)) {cout << "错误:入列失败,队列满" << endl;return false;}// 入列Q->Rear = (Q->Rear+1) % Q->MaxSize;Q->Data[Q->Rear] = X;return true;
}bool IsEmpty(Queue Q) {return Q->Front == Q->Rear;
}ElementType DeleteQ(Queue Q) {if(IsEmpty(Q)) {// cout << "错误:出列失败,队列空" << endl;return ERROR;}// 出列Q->Front = (Q->Front+1) % Q->MaxSize;return Q->Data[Q->Front];
}int QLength(Queue Q) {// 返回队列元素个数if(Q->Front <= Q->Rear)return Q->Rear - Q->Front;return Q->Rear + Q->MaxSize - Q->Front;
}int main(int argc, char *argv[]) {Queue Q = CreateQueue(6); // 易错,最大容量记得加一int n, cmd;ElementType X;cin >> n;while(n--) {cin >> cmd;if(cmd == 1) {// 入队cin >> X;AddQ(Q, X);}else {// 出队cout << DeleteQ(Q) << ' ';}}cout << endl << QLength(Q) << endl;while (!IsEmpty(Q)) {cout << DeleteQ(Q) << ' ';}
}
若要求使用链式储存,则代码如下:
/* 队列的链式储存 7-2 实验2-2(循环队列)* 与队列的顺序存储不同,队列的链式存储入队出队实际上就是在一个链表的尾部插入结点或者头部删除结点* 队列的头指向链表的头结点,队列的尾指向链表的尾结点*/#include <iostream>
#define ERROR -1 // 自定义错误返回值
using namespace std;typedef int ElementType;
typedef struct Node * PtrToNode;
struct Node { // 链表节点,相当于有了链表才有链式的队列ElementType Data;PtrToNode Next;
};
typedef PtrToNode Position;typedef struct QNode * PtrToQNode;
struct QNode { // 队列节点Position Front, Rear;int MaxSize; // 设置队列最大容量(理论上无最大)int Length; // 当前元素个数
};
typedef PtrToQNode Queue;Queue CreatQueue(int MaxSize) {// 创建链式队列Queue Q = (Queue)malloc(sizeof(struct QNode));Q->Front = Q->Rear = NULL;Q->MaxSize = MaxSize;Q->Length = 0;return Q;
}bool IsEmpty(Queue Q) {return Q->Front == NULL; // 头结点为空,队列为空
}bool IsFull(Queue Q) {// 为了功能一致,加入最大容量限制return Q->Length >= Q->MaxSize;
}bool AddQ(Queue Q, ElementType X) {if(IsFull(Q)) {// cout << "错误:入队失败,队满" << endl;return false;}// 入队Position NewNode = (Position)malloc(sizeof(struct Node));NewNode->Data = X;NewNode->Next = NULL;if(IsEmpty(Q)) {// 队列为空时,对头对尾指向该新节点Q->Front = Q->Rear = NewNode;}else {// 队列非空则对尾加入结点Q->Rear->Next = NewNode;Q->Rear = NewNode; // 更新队尾}Q->Length++;return true;
}ElementType DleteQ(Queue Q) {if(IsEmpty(Q)) {// cout << "错误:出队失败,队空" << endl;return ERROR;}// 出队Position OutNode = Q->Front;ElementType OutElement = OutNode->Data;if(Q->Front == Q->Rear) { // 若队列只有一个元素Q->Front = Q->Rear = NULL; // 对头对尾置空,队列空} else {Q->Front = Q->Front->Next;}free(OutNode);Q->Length--;return OutElement;
}int main(int argc, char *argv[]) {int n, cmd;ElementType X;Queue Q = CreatQueue(5); // 由题设置最大容量cin >> n;while (n--) {cin >> cmd;if(cmd == 1) {// 入队指令cin >> X;AddQ(Q,X);}else {// 出队指令cout << DleteQ(Q) << ' ';}}cout << endl << Q->Length << endl;while (!IsEmpty(Q)) {cout << DleteQ(Q) << ' ';}
}