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

    循环队列并不复杂,把几个位置规定清楚是关键!比如,front和rear各指向哪?还有一些小细节,首先观察图:

    从书上来看,尾端rear必定为空(特意留下一个空位置),front总是指向队头,而rear指向尾。
    接着是几个重要的公式

    判定队列满的公式

    队列长度的计算公式

    下面以入队列说明,首先判定队列是否满?第二步,在rear位置处插入数据,最后计算rear的下标,出列与之相似。

    #include <iostream>
    using namespace std;
    #define OK 1
    #define ERROR 0
    #define QueueSize 100
    typedef struct
    {
        int data[QueueSize];
        int front,rear;
    }*SeqQueue,Squeue;
    void InitQueue(SeqQueue &Q)/*初始化*/
    {
        Q->front=Q->rear=0;
    }
    int EnQueue(SeqQueue &Q,int e)/*入队*/
    {
        if(Q->front==(Q->rear+1)%QueueSize)/*队列是否满?*/
            return ERROR;
        Q->data[Q->rear]=e;
        Q->rear=(Q->rear+1)%QueueSize;
        return OK;
    }
    int DeQueue(SeqQueue &Q,int &e)/*出队*/
    {
        if(Q->front==Q->rear)/*队列是否空*/
        {
            cout<<"队列已空!"<<endl;
            return ERROR;
        }
        e=Q->data[Q->front];
        Q->front=(Q->front+1)%QueueSize;
        return OK;
    }
    int main()
    {
        int e;
        SeqQueue q=(SeqQueue)malloc(sizeof(Squeue));/*堆创建*/
        InitQueue(q);/*初始化*/
        EnQueue(q,12);/*入队*/
        DeQueue(q,e);/*出队*/
        cout<<"e="<<e<<endl;
        cout<<"font="<<q->front<<endl;
        cout<<"rear="<<q->rear<<endl;
        DeQueue(q,e);/*出队*/
        free(q);
        return 0;
    }
  • 相关阅读:
    hdu2243之AC自动机+矩阵乘法
    jstl
    HDU 3360 National Treasures 奇偶匹配的最低点覆盖
    【UVA】11992
    what is the difference between definition and declaration in c
    UVALive 6485 Electric Car Rally (BFS,PQ)
    Android发展_备份短信
    vxWorks 命令
    【C++智能指针 auto_ptr】
    Get and Post(Unity3D六个发展)
  • 原文地址:https://www.cnblogs.com/tinaluo/p/5240729.html
Copyright © 2011-2022 走看看