• 数据结构C语言实现----循环队列


    代码如下:

    #include<stdio.h>
    #include<stdlib.h>
    
    typedef char ElemType;
    #define MAXQUEUE 100
    
    typedef struct
    {
        ElemType *base;
        int front;
        int rear;
    }cycleQueue;
    
    /////////////////////////////////
    //创建一个循环队列
    void initqueue(cycleQueue *q)
    {
        q->base = (ElemType*)malloc(sizeof(cycleQueue) * MAXQUEUE);//为循环队列申请连续空间
        if (!q->base)
        {
            exit(0);
        }
        q->front = q->rear = 0;//初始换队首队尾位置为0
    }
    
    /////////////////////////////////
    //入循环队列
    void EnQueue(cycleQueue *q , ElemType e)
    {
        if ((q->rear+1) % MAXQUEUE== q->front)
        {
            return;
        }
        q->base[q->rear] = e;
        q->rear = (q->rear+1)%MAXQUEUE;
    }
    
    ///////////////////////////////////
    //出循环列表
    void DeQueue(cycleQueue *q , ElemType *e)
    {
        if (q->rear == q->front)
        {
            return;
        }
        *e = q->base[q->front];
        q->front = (q->front+1)%MAXQUEUE;
    }
    
    int main()
    {
        cycleQueue q;
        initqueue(&q);
        printf("正在创建循环队列...
    创建成功!
    ");
    
        printf("请输入存入循环队列的数据:");
        ElemType e;
        while ((e = getchar())!='
    ')
        {
            if (e!='
    ')
            {
                EnQueue(&q,e);
            }
        }
        
        printf("正在打印循环队列...
    当前循环队列为:");
        for (size_t i = 0; i < q.rear; i++)
        {
            printf("%c",q.base[q.front+i]);
        }
        putchar('
    ');
    
        printf("请输入要从队头出队列几个元素:");
        int c;
        scanf("%d",&c);
        while (c)
        {
            DeQueue(&q , &e);
            printf("%c已出循环队列!
    ",e);
            c--;
        }
    
        printf("正在打印循环队列...
    当前循环队列为:");
        for (size_t i = 0; i < q.rear; i++)
        {
            printf("%c",q.base[q.front+i]);
        }
        putchar('
    ');
        return 0;
    }
    

      

    运行结果:

  • 相关阅读:
    经典的阿里前端笔试题
    Javascript之浏览器兼容EventUtil
    Javascript之对象的创建
    奇妙的CSS之CSS3新特性总结
    前端优化之无阻塞加载脚本
    正则表达式规则与常见的正则表达式
    全端工程师
    最全前端面试问题及答案总结--《转载》
    奇妙的CSS之布局与定位
    关于在django框架里取已登录用户名字的问题
  • 原文地址:https://www.cnblogs.com/jerryleesir/p/13340286.html
Copyright © 2020-2023  润新知