循环队列的常见操作

#include <iostream>

using namespace std;

const int MAXSIZE=20;  //设置队列最大尺寸

struct Squeue          //队列基本格式

{

  int data[20];

  int front;

  int rear;

};

void InitQueue(Squeue *t)    //队列初始化

{

    t->front=0;

    t->rear=0;

}

int  QueueLength(Squeue *t)  //队列实际大小计算

{

    return (t->rear-t->front+MAXSIZE)%MAXSIZE;

}

void EnQueue(Squeue *t)    //入队操作;只需要判断队列是否为队满状态即可

{

    if((t->rear+1)%MAXSIZE==t->front)

        return ;

    else

    {

        cout<<"请输入你将要入队的元素:"<<endl;

        cin>>t->data[t->rear];

        t->rear=(t->rear+1)%MAXSIZE;

    }

}

void DeQueue(Squeue *t)    //出队,需要判断队列是否为空,因为为空便不可进行出队操作了。

{

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

        return;

    else

    {

      cout<<t->data[t->front]<<endl;

      t->front=(t->front+1)%MAXSIZE;  //取余的运算实际上是在看该结果属于哪个集合的哪个部分

      cout<<t->front<<endl;

      cout<<t->rear<<endl;

    }

}

int main()             

{

    Squeue A;

    Squeue *p=&A;

    InitQueue(p);

    int a=QueueLength(p);

    cout<<"队列长度为:"<<a<<endl;

    EnQueue(p);

    a=QueueLength(p);

    cout<<"队列长度为:"<<a<<endl;

    DeQueue(p);

    return 0;

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 题目类型 a.C++与C差异(1-18) 1.C和C++中struct有什么区别? C没有Protection行为...
    阿面a阅读 12,300评论 0 10
  • #include #include<conio.h> //控制台数据输入输出的函数 #include<fstrea...
    黑键_阅读 4,949评论 0 1
  • 这个程序设计了5个类,分别是 队列结点类( class QueueNode ),因为该队列将来会用来存储树的结点 ...
    锋芒工作室阅读 2,773评论 0 0
  • #include #include<cstring> #include<cstdlib> #include<fst...
    屈大帅阅读 4,185评论 0 2
  • 上章简介:尉迟少恭突然说要离开京城,我变得无比失落,而此时,一个被称为成王爷的人来到了客栈, 他竟然提出要让我陪他...
    苏寂然阅读 2,586评论 0 0