zoukankan      html  css  js  c++  java
  • D_S 循环队列的基本操作

    //  main.cpp

    #include <iostream>

    using namespace std;

    #include "Status.h"

    typedef int QElemType;

    #include "SqQueue.h"

    int main()

    {

        SqQueue Q;

        QElemType e;

        InitQueue(Q);

        EnQueue(Q,1);

        EnQueue(Q,3);

        EnQueue(Q,5);

        EnQueue(Q,7);

        cout<<" 队头元素为:"<<GetHead(Q,e)<<endl;

        cout<<" 队列中从队头至队尾的元素为:";

        QueueTraverse(Q);

        cout<<" 队列的长度为:"<<QueueLength(Q)<<endl;

        DeQueue(Q,e);

        cout<<" 出队的元素为:"<<e<<endl;

        cout<<" 出队操作完成之后队列中从队头至队尾的元素为:";

        QueueTraverse(Q);

        cout<<" 队列的长度为:"<<QueueLength(Q)<<endl;

        return 0;

    }

    //  Status.h

    #ifndef yuan_Status_h

    #define yuan_Status_h

    #define TRUE 1

    #define FALSE 0

    #define OK 1

    #define ERROR 0

    #define INFEASIBLE -1

    #define OVERFLOW -2

    typedef int Status;

    #endif

    //  SqQueue.h

    #ifndef Y_Y_SqQueue_h

    #define Y_Y_SqQueue_h

    #define MAXQSIZE 100

    typedef struct{

        QElemType *base;

        int front;

        int rear;  

    }SqQueue;

    Status InitQueue(SqQueue &Q)

    {

        Q.base=new QElemType[MAXQSIZE];

        if(!Q.base)  exit(OVERFLOW);

        Q.front=Q.rear=0;

        return OK;

    }

    Status GetHead(SqQueue Q,QElemType e)

    {

        e=Q.base[Q.front];

        return e;

    }

    Status QueueLength(SqQueue Q)

    {

        return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;

    }

    Status EnQueue(SqQueue &Q,QElemType e)

    {

        if ((Q.rear+1)%MAXQSIZE==Q.front) return ERROR;  //尾指针在循环意义上加1后等于头指针,表示满队

        Q.base[Q.rear]=e;

        Q.rear=(Q.rear+1)%MAXQSIZE;

        return OK;

    }

    Status DeQueue(SqQueue &Q,QElemType &e)

    {

        if (Q.front==Q.rear) return ERROR;

        e=Q.base[Q.front];

        Q.front=(Q.front+1)%MAXQSIZE;

        return OK;

    }

    void QueueTraverse(SqQueue Q)

    {

        int i;

        i=Q.front;

        while(i!=Q.rear)

        {

            cout<<Q.base[i]<<"  ";

            i=(i+1)%MAXQSIZE;

        }

        cout<<endl;

    }

    #endif

  • 相关阅读:
    wap2app(五)-- 微信授权登录以及踩过的坑
    wap2app(六)-- wap2app的原生标题头无法隐藏
    创建对象的几种模式
    通过url动态获取图片大小方法总结
    wap2app(三)-- 添加引导页
    wap2app(二)-- 设置APP系统状态栏
    wap2app(一)-- 网站快速打包成app
    树叶飘落、雪花飘落等同时多个图片飘落
    基于bootstrap的双日历插件 daterangepicker
    FileProvider解决FileUriExposedException
  • 原文地址:https://www.cnblogs.com/YuanYe1/p/5014704.html
Copyright © 2011-2022 走看看