zoukankan      html  css  js  c++  java
  • 循环队列-简单

    源程序:

    //循环队列

    #include <stdio.h>

    #include <stdlib.h>

    #define MAXSIZE 100

    typedef struct cycqueue

    {

    int data[MAXSIZE];

    int front,rear;   //声明队列的头指针和尾指针

    }CycQue;  //队列的数据类型

    //初始化循环队列

    void InitQueue(CycQue *CQ)

    {

    CQ->front=0;

    CQ->rear=0;

    }

    //判断队列是否为空

    int EmptyQueue(CycQue *CQ)

    {

    if(CQ->front==CQ->rear)

    return 1;          //队列为空,即为真

    else

    return 0;

    }

    //入队操作

    int EnQueue(CycQue *CQ,int x)

    {

    if((CQ->rear+1) % MAXSIZE == CQ->front) //判断队列是否已满

    {

    printf("队列已满! ");

    return 0;

    }

    else

    {

    CQ->rear=(CQ->rear+1) % MAXSIZE;//如果队列没满,尾指针向后移动一个单元

    CQ->data[CQ->rear]=x;

    return 1;

    }

    }

    //出队操作

    void OutQueue(CycQue *CQ)

    {

    if(EmptyQueue(CQ))  //首先判断队列是否为空

    {

    printf("空队列! ");

    //return 0;

    }

    else

    {

    CQ->front=(CQ->front+1) % MAXSIZE;

    }

    }

    //取栈顶元素

    int GetHead(CycQue *CQ)

    {

    if(EmptyQueue(CQ))

    return 0;

    else

    return CQ->data[(CQ->front+1) % MAXSIZE];

    }

    int main()

    {

    CycQue CQ;

    CycQue *q=&CQ;

    int i,n,e;

    InitQueue(&CQ);

    for(i=0;i<5;i++)

    {

    scanf("%d",&n);

    e=EnQueue(&CQ,n);

    }

    for(i=0;i<5;i++)

    {

    if(!EmptyQueue(&CQ))  //!代表非

    {

    n=GetHead(&CQ);

    OutQueue(&CQ);

    printf("%d ",n);

    }

    else

    printf(" 空队列! ");

    }

    return 0;

    }

  • 相关阅读:
    CentOS 7.4 如何安装 MariaDB 10.3.9 Stable 数据库
    xxx is not in the sudoers file. This incident will be reported.
    CentOS 7.4 上如何安装 tomcat 9
    CentOS 7.4 下面安装 jdk 10 的一点总结
    CentOS 7.4 下安装 Nginx
    MySQL数据库常用操作
    chart学习
    Ext需要的文件目录
    获取浏览器信息
    运行容器
  • 原文地址:https://www.cnblogs.com/duanqibo/p/14716790.html
Copyright © 2011-2022 走看看