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) << ' ';
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Qhumaing

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

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

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

打赏作者

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

抵扣说明:

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

余额充值