• 循环队列


      队列(queue)是只允许在一端进行插入操作,而在另一端进行删除操作的线性表。允许插入的一端为队尾(入队),允许删除(出队)的一端为队头。

      顺序存储的队列是采用数组来实现的,但由于数组是静态的,在队列的出队和入队的操作下会出现整个队列后移逐渐占用下标加大位置而下标较小位置为空的“假溢出”现象,所以采用了将存储队列的数组看成是头尾相接的循环结构,即允许队列直接从数组的下标最大的位置延续到下标最小的位置。

        

      判断队列的状态的标准是头指针与尾指针的关系。约定队头指针front指向队头元素的前一个位置,队尾指针rear指向队尾元素。为了区分队满与队空,采取浪费一个数组元素的空间的策略:

        队空:front==rear

        队满:(rear+1)%QueueSize==front

    C++实现如下:

    #include <bits/stdc++.h>
    using namespace std;
    
    class Node
    {
    public:
        int data=-1;
        Node *next;
    };
    
    class R_linklist
    {
    public:
        Node *front,*tail;
    
        void init(int n)//初始化队列长度
        {
            if(n==0) return;
            else
            {
                tail=new Node();
                front=new Node();
                Node *temp=new Node();
    
                tail=front;
                temp=front;
    
                Node *t;
                for(int i=1;i<n;i++)
                {
                    t=new Node();
                    temp->next=t;
                    temp=temp->next;
                }
    
                temp->next=tail;
            }
        }
    
        void push(int i)//进队
        {
            if(tail->next==front)
            {
                cout<<"循环队列已满!"<<endl;
                return;
            }
            tail->data=i;
            tail=tail->next;
        }
    
        void pop()//出队
        {
            if(tail==front)
            {
                cout<<"循环队列为空!"<<endl;
                return;
            }
            cout<<front->data<<endl;
            front->data=-1;
            front=front->next;
        }
    
        void print()//输出整个循环队列元素
        {
            while(front!=tail)
            {
                cout<<front->data<<' ';
                front=front->next;
            }
            cout<<endl;
        }
    };
    
    int main()
    {
        R_linklist l;
        l.init(5);
        l.push(1);
        l.push(3);
        l.push(4);
        l.print();
        return 0;
    }
  • 相关阅读:
    flask 本地局域网连接
    python
    Python 有关网址
    Python 字典(Dictionary)操作详解
    Elasticsearch集成HanLP分词器-个人学习
    知识图谱构建
    项目实战:如何构建知识图谱
    10分钟上手图数据库Neo4j
    知识图谱技术原理介绍
    知识图谱的应用
  • 原文地址:https://www.cnblogs.com/ybf-yyj/p/9520614.html
Copyright © 2020-2023  润新知