zoukankan      html  css  js  c++  java
  • 数据结构之队列

    队列(Queue),是一种线性存储结构。它有以下几个特点:
    (01) 队列中数据是按照"先进先出(FIFO, First-In-First-Out)"方式进出队列的。
    (02) 队列只允许在"队首"进行删除操作,而在"队尾"进行插入操作。

    一 C++标准库queue

    (1)成员函数

    成员函数 说明 时间复杂度
    size() 返回队列大小 O(1)
    push() 在末尾加入一个元素 O(1)
    pop() 删除第一个元素 O(1)
    front() 返回第一个元素 O(1)
    end() 返回最后一个元素 O(1)
    empty 队列为空返回真 O(1)

    (2)示例

    #include <iostream>
    #include <queue>
    using namespace std;
    
    /**
     * C++ : STL中的队列(queue)的演示程序。
     *
     * @author skywang
     * @date 2013/11/07
     */
    int main ()
    {
        int tmp=0;
        queue<int> iqueue;
    
        // 将10, 20, 30 依次加入队列的末尾
        iqueue.push(10);
        iqueue.push(20);
        iqueue.push(30);
    
        // 删除队列开头的元素
        iqueue.pop();
    
        // 将“队列开头的元素”赋值给tmp,不删除该元素.
        tmp = iqueue.front();
        cout<<"tmp="<<tmp<<endl;
    
        // 将40加入到队列的末尾
        iqueue.push(40);
    
        cout << "empty()=" << iqueue.empty() <<endl;
        cout << "size()=" << iqueue.size() <<endl;
        while (!iqueue.empty()) 
        {
            tmp = iqueue.front();
            cout<<tmp<<endl;
            iqueue.pop();  
        }
    
        return 0;
    }

    二 C++实现队列

    #ifndef ARRAY_QUEUE_HXX
    #define ARRAY_QUEUE_HXX
    
    #include <iostream>
    using namespace std;
    
    template<class T> class ArrayQueue{
        public:
            ArrayQueue();
            ~ArrayQueue();
    
            void add(T t);
            T front();
            T pop();
            int size();
            int is_empty();
    
        private:
            T *arr;
            int count;
    };
    
    // 创建“队列”,默认大小是12
    template<class T>
    ArrayQueue<T>::ArrayQueue() 
    {
        arr = new T[12];
        if (!arr) 
        {
            cout<<"arr malloc error!"<<endl;
        }
    }
    
    // 销毁“队列”
    template<class T>
    ArrayQueue<T>::~ArrayQueue() 
    {
        if (arr) 
        {
            delete[] arr;
            arr = NULL;
        }
    }
    
    // 将val添加到队列的末尾
    template<class T>
    void ArrayQueue<T>::add(T t) 
    {
        arr[count++] = t;
    }
    
    
    // 返回“队列开头元素”
    template<class T>
    T ArrayQueue<T>::front() 
    {
        return arr[0];
    }
    
    // 返回并删除“队列末尾的元素”
    template<class T>
    T ArrayQueue<T>::pop() 
    {
        int i = 0;;
        T ret = arr[0];
    
        count--;
        while (i++<count)
            arr[i-1] = arr[i];
    
        return ret;
    }
    
    // 返回“队列”的大小
    template<class T>
    int ArrayQueue<T>::size() 
    {
        return count;
    }
    
    // 返回“队列”是否为空
    template<class T>
    int ArrayQueue<T>::is_empty()
    {
        return count==0;
    }
    
    #endif
  • 相关阅读:
    如何让pc端网站在手机上可以等比缩放的整个显示
    CSS
    常见的IE布局兼容问题
    CSS : 使用 z-index 的前提
    CSS : object-fit 和 object-position实现 图片或视频自适应
    CSS
    vscode
    如何识别Form字段中一对多或者多对多字段
    window.open简单使用
    由一个模型拿它的名字、app的名字、字段对象以及字段对象中的属性
  • 原文地址:https://www.cnblogs.com/xiaobingqianrui/p/8882088.html
Copyright © 2011-2022 走看看