數據結構——鏈式隊列模板類實現

數據結構筆記3.3.3 Queue
與棧類似,隊列也分成順序隊列和鏈式隊列。用單鏈表表示的鏈式隊列特別適合於元素變動比較大的情形,而且不存在隊列FULL而溢出的情況。另外,假若程序中需要多個隊列,與多個棧的情形一樣,最好使用鏈式隊列。這樣不會出現存儲分配不合理的問題,也不需要考慮存儲的移動。下面就給出相應的代碼。
鏈式隊列模板類代碼:

//數據結構——鏈式隊列模板類
#include <iostream>
using namespace std;
template<class T>
struct LinkNode {
    T data;                 //隊列每個節點的數據域
    LinkNode<T> *next;      //隊列每個節點的指針域
    //構造函數
    LinkNode(T x, LinkNode<T> *p = NULL) {
        data = x;
        next = p;
    }
};
template<class T>
class LinkedQueue {
public:
    LinkedQueue();                          //構造函數
    ~LinkedQueue();                            //析構函數
    bool enQueue(const T & x);              //將x加入隊列當中
    bool delQueue(T & x);                   //刪除隊頭元素,x返回其值
    bool getFront(T & x) const;             //查看隊頭元素的值
    void makeEmpty();                       //將隊列清空
    bool isEmpty() const;                   //判斷隊列是否爲NULL
    int getSize() const;                    //返回隊列元素的個數
    void output(ostream & out);             //輸出隊列元素,由重載運算符函數調用
private:
    LinkNode<T> *front, *rear;              //隊頭、隊尾指針
};

//函數定義
template<class T>
LinkedQueue<T>::LinkedQueue() {
    //構造函數,初始化隊頭和隊尾指針
    front = rear = NULL;
}

template<class T>
void LinkedQueue<T>::makeEmpty() {
    //置空隊列,釋放鏈表中的所有節點
    LinkNode<T> *current;   
    while (front != NULL) {
        current = front;
        front = front->next;
        delete current;
    }
}

template<class T>
bool LinkedQueue<T>::enQueue(const T & x) {
    //元素x進入隊尾
    if (NULL == front) {
        //如果是空隊列,直接用指針開闢節點
        front = rear = new LinkNode<T>(x);
        if (NULL == front) {
            //分配內存失敗返回false
            return false;
        }
    }
    else {
        rear->next = new LinkNode<T>(x);
        if (NULL == rear->next) {       //直接用rear的next開闢新節點
            return false;
        }
        rear = rear->next;              //更新尾指針
    }
    return true;
}

template<class T>
bool LinkedQueue<T>::delQueue(T & x) {
    //如果隊列不是NULL,刪除隊頭節點,函數返回true,否則返回false
    if (isEmpty()) {                    //隊列爲空,出隊失敗
        return false;
    }
    LinkNode<T> *Del = front;
    x = front->data;
    front = front->next;
    delete Del;
    return true;
}

template<class T>
bool LinkedQueue<T>::getFront(T & x)const {
    //若隊列不爲NULL,函數返回隊頭元素的值和true,否則返回false
    if (isEmpty()) {
        return false;
    }
    x = front->data;
    return true;
}

template<class T>
int LinkedQueue<T>::getSize()const {
    //函數返回隊列元素的個數
    LinkNode<T> *p = front;
    int queueEleAmount = 0;
    while (p != NULL) {
        queueEleAmount++;
        p = p->next;
    }
    return queueEleAmount;
}

template<class T>
bool LinkedQueue<T>::isEmpty() const {
    //判斷隊列是否爲NULL並返回true,否則返回false
    if (front == NULL) {
        return true;
    }
    else {
        return false;
    }
}

template<class T>
LinkedQueue<T>::~LinkedQueue() {
    //析構函數,釋放程序中的資源
    makeEmpty();
}

template<class T>
void LinkedQueue<T>::output(ostream & out) {
    //輸出隊列中的元素,被重載<<函數調用
    LinkNode<T> *current = front;
    while (current != NULL) {
        out << current->data << " ";
        current = current->next;
    }
    cout << endl;
}

template<class T>
ostream & operator << (ostream & out, LinkedQueue<T> &LQ) {
    //重載<<運算符函數,調用output實現隊列的輸出
    LQ.output(out);
    return out;
}

Main函數測試代碼:

int main()
{
    LinkedQueue<int> link_queue;
    link_queue.enQueue(1);              //入隊測試
    link_queue.enQueue(2);
    link_queue.enQueue(3);
    link_queue.enQueue(4);
    link_queue.enQueue(5);
    cout << link_queue;
    int del_value1, del_value2;
    link_queue.delQueue(del_value1);    //出隊測試
    link_queue.delQueue(del_value2);
    cout << link_queue;
    int queue_head = 0;
    link_queue.getFront(queue_head);    //讀取隊頭測試
    cout << queue_head << endl;
    link_queue.makeEmpty();             //set NULL test
    if (link_queue.isEmpty()) {
        cout << "Queue is null" << endl;
    }
    cout << link_queue.getSize() << endl;//返回隊列元素個數測試

    system("pause");
    return 0;
}

運行效果:
這裏寫圖片描述

發佈了50 篇原創文章 · 獲贊 228 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章