• c++实现的数组队列


    数组队列类源码:

    #pragma once
    
    template<class T>
    class ArrayQueue
    {
    public:
        ArrayQueue(void)
        {
            front = 0;
            rear = 0;
            maxSize = 20;
            list = new T [maxSize];
        }
    
        //向队尾插入元素
        void in(T obj)
        {
            if(!IsFull())
            {
                list[rear] = obj; 
                rear=(rear+1)%maxSize;
            }
        }
    
        //从队头取出元素
        T out()
        {
            if(!IsEmpty())
            {
                int t= list[front];
                front = (front+1)%maxSize;
                return t;
            }
        }
    
        bool IsEmpty()
        {
            if(front==rear)
                return true;
            else
                return false;
        }
    
        //循环队列,少用一个存储单元,队满条件:front==(rear+1)%maxSize
        bool IsFull()
        {
            if((rear+1)%maxSize==front)
                return true;
            else
                return false;
        }
    
        ~ArrayQueue(void);
    
    private:
        int front;
        int rear;
        int maxSize;
        T *list;
    };
    View Code


    测试代码:

    //数组队列
        ArrayQueue<int> * queue = new ArrayQueue<int>();
        queue->in(3);queue->in(2);queue->in(7);queue->in(6);
        queue->in(8);queue->in(5);queue->in(4);queue->in(1);
        while(!queue->IsEmpty())
        {
            std::cout<<queue->out()<<" ";
        }
    View Code
  • 相关阅读:
    jvm内存模型学习
    sparkstreaming容错
    yum命里详解
    rpm包的安装及卸载
    git命令记录
    ElaticSearch基本查询
    入门elaticsearch
    Java网络编程学习(一)
    JAVA多线程之生产消费模型
    Java线程同步详解
  • 原文地址:https://www.cnblogs.com/xiayangqiushi/p/3339782.html
Copyright © 2020-2023  润新知