严蔚敏数据结构C语言版循环队列的基本操作

一、循环队列的定义方式

#define MAXSIZE 50
typedef int QElemType;
typedef int Status;
typedef struct {
    QElemType* base;
    int front;
    int rear;
    int QueueSize;
}Queue;

二、初始化

Status InitQueue(Queue& Q)
{
    Q.base = (QElemType*)malloc(sizeof(QElemType) * MAXSIZE);
    if (!Q.base)return false;
    Q.front = Q.rear = 0;
    Q.QueueSize = MAXSIZE;
    return true;
}

三、销毁

Status DestroyQueue(Queue& Q)
{
    if (!Q.base)return false;
    free(Q.base);
    Q.base = NULL;
    Q.front = Q.rear = 0;
    return 0;
}

四、清空

Status ClearQueue(Queue Q)
{
    if (!Q.base)return false;
    Q.rear = Q.front = 0;
    return true;
}

五、判空

Status QueueEmpty(Queue Q)
{
    if (Q.front == Q.rear)return true;
    else return false;
}

六、判空

int QueueLength(Queue Q)
{
    return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}

七、入队列

Status EnQueue(Queue& Q, QElemType e)
{
    if ((Q.rear + 1) % MAXSIZE == Q.front)return false;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXSIZE;
    return true;
}

八、出队列

Status DeQueue(Queue& Q,QElemType& e)
{
    if (Q.front == Q.rear)return false;
    e = Q.base[Q.front];
    Q.front = (Q.front + 1) % MAXSIZE;
    return true;
}

九、打印循环队列

void PrintQueue(Queue Q)
{
    int cnt = 0;
    if (Q.rear > Q.front)
        for (int i = Q.front; i < Q.rear; i++)
        {
            cnt++;
            printf("%d ", Q.base[i]);
            if (cnt % 10 == 0)printf("\n");
        }
    else
    {
        for (int i = Q.front; i < Q.QueueSize; i++)
        {
            cnt++;
            printf("%d ", Q.base[i]);
            if (cnt % 10 == 0)printf("\n");
        }
        for (int i = 0; i < Q.rear; i++)
        {
            cnt++;
            printf("%d ", Q.base[i]);
            if (cnt % 10 == 0)printf("\n");
        }
    }
    puts("");
}

十、总代码

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50
typedef int QElemType;
typedef int Status;
typedef struct {
    QElemType* base;
    int front;
    int rear;
    int QueueSize;
}Queue;
void PrintQueue(Queue Q);
Status InitQueue(Queue& Q);
Status DestroyQueue(Queue& Q);
Status ClearQueue(Queue Q);
Status QueueEmpty(Queue Q);
int QueueLength(Queue Q);
Status EnQueue(Queue& Q, QElemType e);
Status DeQueue(Queue& Q, QElemType& e);
int main()
{
    Queue Q;
    QElemType e;
    InitQueue(Q);
    printf("将一班的学号压入队列:\n");
    for (int i = 19422101; i <= 19422130; i++)
        EnQueue(Q, i);
    PrintQueue(Q);
    printf("前十五个同学出队列:\n");
    for (int i = 1; i <= 15; i++)
    {
        DeQueue(Q, e);
        printf("%d ", e);
        if (i % 10 == 0)printf("\n");
    }
    printf("\n\n");
    printf("剩下的十五个同学学号分别是:\n");
    PrintQueue(Q); puts("");
    printf("将二班的学号压入队列:\n");
    for (int i = 19422201; i <= 19422230; i++)
        EnQueue(Q, i);
    PrintQueue(Q);
    DestroyQueue(Q);
    return 0;
}
void PrintQueue(Queue Q)
{
    int cnt = 0;
    if (Q.rear > Q.front)
        for (int i = Q.front; i < Q.rear; i++)
        {
            cnt++;
            printf("%d ", Q.base[i]);
            if (cnt % 10 == 0)printf("\n");
        }
    else
    {
        for (int i = Q.front; i < Q.QueueSize; i++)
        {
            cnt++;
            printf("%d ", Q.base[i]);
            if (cnt % 10 == 0)printf("\n");
        }
        for (int i = 0; i < Q.rear; i++)
        {
            cnt++;
            printf("%d ", Q.base[i]);
            if (cnt % 10 == 0)printf("\n");
        }
    }
    puts("");
}
Status InitQueue(Queue& Q)
{
    Q.base = (QElemType*)malloc(sizeof(QElemType) * MAXSIZE);
    if (!Q.base)return false;
    Q.front = Q.rear = 0;
    Q.QueueSize = MAXSIZE;
    return true;
}
Status DestroyQueue(Queue& Q)
{
    if (!Q.base)return false;
    free(Q.base);
    Q.base = NULL;
    Q.front = Q.rear = 0;
    return 0;
}
Status ClearQueue(Queue Q)
{
    if (!Q.base)return false;
    Q.rear = Q.front = 0;
    return true;
}
Status QueueEmpty(Queue Q)
{
    if (Q.front == Q.rear)return true;
    else return false;
}
int QueueLength(Queue Q)
{
    return (Q.rear - Q.front + MAXSIZE) % MAXSIZE;
}
Status EnQueue(Queue& Q, QElemType e)
{
    if ((Q.rear + 1) % MAXSIZE == Q.front)return false;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear + 1) % MAXSIZE;
    return true;
}
Status DeQueue(Queue& Q,QElemType& e)
{
    if (Q.front == Q.rear)return false;
    e = Q.base[Q.front];
    Q.front = (Q.front + 1) % MAXSIZE;
    return true;
}

运行效果:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AAAAAZBX

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

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

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

打赏作者

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

抵扣说明:

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

余额充值