隊列的實現——c++


一、介紹

隊列(Queue),是一種線性存儲結構。它有以下幾個特點:
(01) 隊列中數據是按照"先進先出(FIFO, First-In-First-Out)"方式進出隊列的。
(02) 隊列只允許在"隊首"進行刪除操作,而在"隊尾"進行插入操作。
隊列通常包括的兩種操作:入隊列 和 出隊列

 

二、實現

C++的STL中本身就包含了list類,基本上該list類就能滿足我們的需求,所以很少需要我們自己來實現。本部分介紹2種C++實現。
1. C++實現一:數組實現的隊列,能存儲任意類型的數據。
2. C++實現二:C++的 STL 中自帶的"隊列"(list)的示例。

 

1.C++實現一:數組實現的隊列,能存儲任意類型的數據。

實現代碼:.h

#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
View Code

測試代碼: .cpp

#include <iostream>
#include "ArrayQueue.h"
using namespace std;

/**
 * C++ : 數組實現“隊列”,能存儲任意數據。
 *
 * @author skywang
 * @date 2013/11/07
 */
int main() 
{
    int tmp=0;
    ArrayQueue<int> *astack = new ArrayQueue<int>();

    // 將10, 20, 30 依次推入隊列中
    astack->add(10);
    astack->add(20);
    astack->add(30);

    // 將“隊列開頭元素”賦值給tmp,並刪除“該元素”
    tmp = astack->pop();
    cout<<"tmp="<<tmp<<endl;

    // 只將“隊列開頭的元素”賦值給tmp,不刪除該元素.
    tmp = astack->front();
    cout<<"tmp="<<tmp<<endl;

    astack->add(40);

    cout<<"is_empty()="<<astack->is_empty()<<endl;
    cout<<"size()="<<astack->size()<<endl;
    while (!astack->is_empty())
    {
        tmp = astack->pop();
        cout<<tmp<<endl;
    }

    return 0;
}
View Code

 

2. C++實現二:C++的 STL 中自帶的"隊列"(list)的示例

#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;
}

 

 

本文來自http://www.cnblogs.com/skywang12345/p/3562279.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM