數據結構(長期)

 C++實現雙鏈表node

實現代碼
雙向鏈表文件(DoubleLink.h)ios

#ifndef DOUBLE_LINK_HXX
#define DOUBLE_LINK_HXX

#include <iostream>
using namespace std;

template<class T> 
struct DNode 
{
    public:
        T value;
        DNode *prev;
        DNode *next;
    public:
        DNode() { }
        DNode(T t, DNode *prev, DNode *next) {
            this->value = t;
            this->prev  = prev;
            this->next  = next;
           }
};

template<class T> 
class DoubleLink 
{
    public:
        DoubleLink();
        ~DoubleLink();

        int size();
        int is_empty();

        T get(int index);
        T get_first();
        T get_last();

        int insert(int index, T t);
        int insert_first(T t);
        int append_last(T t);

        int del(int index);
        int delete_first();
        int delete_last();

    private:
        int count;
        DNode<T> *phead;
    private:
        DNode<T> *get_node(int index);
};

template<class T>
DoubleLink<T>::DoubleLink() : count(0)
{
    // 建立「表頭」。注意:表頭沒有存儲數據!
    phead = new DNode<T>();
    phead->prev = phead->next = phead;
    // 設置鏈表計數爲0
    //count = 0;
}

// 析構函數
template<class T>
DoubleLink<T>::~DoubleLink() 
{
    // 刪除全部的節點
    DNode<T>* ptmp;
    DNode<T>* pnode = phead->next;
    while (pnode != phead)
    {
        ptmp = pnode;
        pnode=pnode->next;
        delete ptmp;
    }

    // 刪除"表頭"
    delete phead;
    phead = NULL;
}

// 返回節點數目
template<class T>
int DoubleLink<T>::size() 
{
    return count;
}

// 返回鏈表是否爲空
template<class T>
int DoubleLink<T>::is_empty() 
{
    return count==0;
}

// 獲取第index位置的節點
template<class T>
DNode<T>* DoubleLink<T>::get_node(int index) 
{
    // 判斷參數有效性
    if (index<0 || index>=count)
    {
        cout << "get node failed! the index in out of bound!" << endl;
        return NULL;
    }

    // 正向查找
    if (index <= count/2)
    {
        int i=0;
        DNode<T>* pindex = phead->next;
        while (i++ < index) {
            pindex = pindex->next;
        }

        return pindex;
    }

    // 反向查找
    int j=0;
    int rindex = count - index -1;
    DNode<T>* prindex = phead->prev;
    while (j++ < rindex) {
        prindex = prindex->prev;
    }

    return prindex;
}

// 獲取第index位置的節點的值
template<class T>
T DoubleLink<T>::get(int index) 
{
    return get_node(index)->value;
}

// 獲取第1個節點的值
template<class T>
T DoubleLink<T>::get_first() 
{
    return get_node(0)->value;
}

// 獲取最後一個節點的值
template<class T>
T DoubleLink<T>::get_last() 
{
    return get_node(count-1)->value;
}

// 將節點插入到第index位置以前
template<class T>
int DoubleLink<T>::insert(int index, T t) 
{
    if (index == 0)
        return insert_first(t);

    DNode<T>* pindex = get_node(index);
    DNode<T>* pnode  = new DNode<T>(t, pindex->prev, pindex);
    pindex->prev->next = pnode;
    pindex->prev = pnode;
    count++;

    return 0;
}

// 將節點插入第一個節點處。
template<class T>
int DoubleLink<T>::insert_first(T t) 
{
    DNode<T>* pnode  = new DNode<T>(t, phead, phead->next);
    phead->next->prev = pnode;
    phead->next = pnode;
    count++;

    return 0;
}

// 將節點追加到鏈表的末尾
template<class T>
int DoubleLink<T>::append_last(T t) 
{
    DNode<T>* pnode = new DNode<T>(t, phead->prev, phead);
    phead->prev->next = pnode;
    phead->prev = pnode;
    count++;

    return 0;
}

// 刪除index位置的節點
template<class T>
int DoubleLink<T>::del(int index) 
{
    DNode<T>* pindex = get_node(index);
    pindex->next->prev = pindex->prev;
    pindex->prev->next = pindex->next;
    delete pindex;
    count--;

    return 0;
}

// 刪除第一個節點
template<class T>
int DoubleLink<T>::delete_first() 
{
    return del(0);
}

// 刪除最後一個節點
template<class T>
int DoubleLink<T>::delete_last() 
{
    return del(count-1);
}

#endif
相關文章
相關標籤/搜索