zoukankan      html  css  js  c++  java
  • 循环队列--忘记分配空间和如何用tag判断队空队满

    #include<iostream>
    #define maxsize 100
    using namespace std;
    struct CLqueue
    {
        int *Q;
        int front;
        int rear;
        int tag;
    };
    typedef struct CLqueue *CL;
    void Init(CL &q)
    {
        q = new CLqueue;
        q->Q=new int[maxsize];
        q->tag = 0;//队空
        q->rear = q->front=0;
    }
    bool Judge(CL &q)
    {
        if (q->tag)
            return true;
        else
            return false;
    
    }
    void Enqueue(CL &q,int a)
    {
        if (Judge(q))
            return;
        q->Q[q->rear] = a;
        q->rear = (q->rear +1 ) % maxsize;
        if ((q->rear + 1) % maxsize == q->front)
            q->tag = 1;
    
    }
    void Dlqueue(CL &q)
    {
        if (!Judge(q))
            return;
        q->front = (q->front + 1) % maxsize;
        if ((q->front + 1) % maxsize == q->rear)
            q->tag = 0;
    }
    
    
    int main()
    {
        int n;
        CL q;
        while (cin >> n && n != 0)
        {
            Init(q);
            for (int i= 0; i < n; i++)
            {
                int a;
                cin >> a;
                Enqueue(q, a);
            }
            for (int i = q->front; i < q->rear-1; i++)
            {
                cout << q->Q[i] << " ";
            }
            cout<< q->Q[q->rear-1] << endl;
        }
        return 0;
    }
    #include<iostream>
    #define maxsize 100
    using namespace std;
    struct CLqueue
    {
        int *Q;
        int front;
        int rear;
        int tag;
    };
    typedef struct CLqueue *CL;
    void Init(CL &q)
    {
        q = new CLqueue;
        q->Q=new int[maxsize];
        q->tag = 0;//队空
        q->rear = q->front=0;
    }
    bool Judge(CL &q)
    {
        if (q->tag)
            return true;
        else
            return false;
    
    }
    void Enqueue(CL &q,int a)
    {
        if (Judge(q))
            return;
        q->Q[q->rear] = a;
        q->rear = (q->rear +1 ) % maxsize;
        if ((q->rear + 1) % maxsize == q->front)
            q->tag = 1;
    
    }
    void Dlqueue(CL &q)
    {
        if (!Judge(q))
            return;
        q->front = (q->front + 1) % maxsize;
        if ((q->front + 1) % maxsize == q->rear)
            q->tag = 0;
    }
    
    
    int main()
    {
        int n;
        CL q;
        while (cin >> n && n != 0)
        {
            Init(q);
            for (int i= 0; i < n; i++)
            {
                int a;
                cin >> a;
                Enqueue(q, a);
            }
            for (int i = q->front; i < q->rear-1; i++)
            {
                cout << q->Q[i] << " ";
            }
            cout<< q->Q[q->rear-1] << endl;
        }
        return 0;
    }
  • 相关阅读:
    [洛谷P1886]滑动窗口 (单调队列)(线段树)
    树状数组详细解析
    离散化
    kettle使用总结(一)
    springBoot开发的web项目打war包部署到已有的tomcat容器中
    集群时钟同步
    linux服务器安装nginx
    Tomcat学习之体系架构
    码云新建仓库上传项目
    linux系统安装mysql yum方式
  • 原文地址:https://www.cnblogs.com/h694879357/p/11764738.html
Copyright © 2011-2022 走看看