【数据结构】循环列表的基本操作

本文介绍了一种基于数组的循环队列实现方法,并通过C语言代码详细展示了如何初始化队列、判断队列长度、入队及出队操作。此外还提供了一个简单的交互式界面供用户进行队列操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MaxSize 100
typedef struct Queue
{
    int front;
    int rear;
    int *queue;
}Queue,SqQueue;
int InitQueue(SqQueue *Q)
{
    Q->queue = (int *)malloc(MaxSize*sizeof(int));
    if (!Q->queue)
    {
        exit(1);
    }
    Q->front = Q->rear = 0;
    return 0;
}
int QueueLength(SqQueue *Q)
{
    return(Q->rear - Q->front + MaxSize) % MaxSize;
}
int EnterQueue(SqQueue *Q,int e)
{
    if ((Q->queue[Q->rear + 1])%MaxSize == Q->queue[Q->front])
    {
        printf("队列已满!");
        exit(1);
    }
    Q->queue[Q->rear] = e;
    Q->rear = (Q->rear+1) % MaxSize;
    return 0;
}
int DeleteQueue(SqQueue *Q,int *E)
{
    int e;
    if (Q->front == Q->rear)
    {
        printf("队列为空!");
    }
    *E= Q->queue[Q->front];
    Q->front = (Q->front + 1) % MaxSize;
    return 0;
}
int main()
{
    int S,D,F;
    int i = 1, e, Len, data;
    SqQueue * Que;
    Que = (SqQueue *)malloc(sizeof(Queue));
    InitQueue(Que);
    printf("是否想往队列中加点东东嘞?\n");
    printf("是输入1,否输入0\n");
    scanf_s("%d", &S);
    while (S=1)
    {
        printf("请输入第%d个数据:", i++);
        scanf_s("%d", &e);
        EnterQueue(Que, e);
        printf("是否想要继续输入到队列之中?是选择1,否选择0\n");
        scanf_s("%d", &D);
        if (D == 0)
        {
            break;
        }
    }
    printf("看看现在的队列有多长吧!\n");
    Len = QueueLength(Que);
    printf("现在的队列的长度为:%d\n", Len);
    printf("是否想要删除队列之中的一些数据呢?选择1或者0\n");
    scanf_s("%d",& F);
    if ( F=1)
    {
        DeleteQueue(Que,&data);
        printf("删除的数据为:%d\n", data);
    }
    printf("看看现在的队列有多长吧!\n");
    Len = QueueLength(Que);
    printf("现在的队列的长度为:%d\n", Len);
}

 

转载于:https://www.cnblogs.com/code-wangjun/p/4372847.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值