雙向循環鏈表(二十一)

        我們在上節博客中介紹了 Linux 中的內核鏈表,下來我們就用 Linux 內核鏈表來實現 DTLib 中的雙向循環鏈表。它繼承自 DualLinkList,它的繼承關係如下圖所示

圖片.png

        下來我們來講講它的設計思路:數據結點之間在邏輯上構成雙向循環鏈表,頭結點僅用於結點的定位。如下圖所示

圖片.png

        實現思路:

        1、通過模板定義 DualCircleList 類。繼承自 DualLinkList 類;

        2、在 DualCircleList 內部使用 Linux 內核鏈表進行實現;

        3、使用 struct list_head 定義 DualCircleList 的頭結點;

        4、特殊處理:循環遍歷時忽略頭結點


        實現要點:

        1、通過 list_head 進行目標結點定位(position(i));

        2、通過 list_entry 將 list_head 指針轉換爲目標結點指針;

        3、通過 list_for_each 實現 int find(const T& e) 函數;

        4、遍歷函數中的 next() 和 pre() 需要考慮跳過頭結點


        下來我們來看看基於 Linux 內核鏈表的雙向循環鏈表是怎樣寫的

DualCircleList 源碼

#ifndef DUALCIRCLELIST_H
#define DUALCIRCLELIST_H

#include "DualLinkList.h"
#include "LinuxList.h"

namespace DTLib
{

template < typename T >
class DualCircleList : public DualLinkList<T>
{
protected:
    struct Node : public Object
    {
        list_head head;
        T value;
    };

    list_head m_header;
    list_head* m_current;

    list_head* position(int i) const
    {
        list_head* ret = const_cast<list_head*>(&m_header);

        for(int p=0; p<i; p++)
        {
            ret = ret->next;
        }

        return ret;
    }

    int mod(int i) const
    {
        return (this->m_length == 0) ? 0 : (i % this->m_length);
    }
public:
    DualCircleList()
    {
        this->m_length = 0;
        this->m_step = 1;

        m_current = NULL;

        INIT_LIST_HEAD(&m_header);
    }

    bool insert(const T& e)
    {
        return insert(this->m_length, e);
    }

    bool insert(int i, const T& e)
    {
        bool ret = true;
        Node* node = new Node();

        i = i % (this->m_length + 1);

        if(node != NULL)
        {
            node->value = e;

            list_add_tail(&node->head, position(i)->next);

            this->m_length++;
        }
        else
        {
            THROW_EXCEPTION(NoEnoughMemoryException, "No memory to insert new element ...");
        }

        return ret;
    }

    bool remove(int i)
    {
        bool ret = true;

        i = mod(i);

        ret = ((0 <= i) && (i < this->m_length));

        if( ret )
        {
            list_head* toDel = position(i)->next;

            if( m_current == toDel )
            {
                m_current = toDel->next;
            }

            list_del(toDel);

            this->m_length--;

            delete list_entry(toDel, Node, head);
        }

        return ret;
    }

    bool set(int i, const T& e)
    {
        bool ret = true;

        i = mod(i);

        ret = ((0 <= i) && (i < this->m_length));

        if( ret )
        {
            list_entry(position(i)->next, Node, head)->value = e;
        }

        return ret;
    }

    T get(int i) const
    {
        T ret;

        if( get(i, ret) )
        {
            return ret;
        }
        else
        {
            THROW_EXCEPTION(IndexOutOfBoundsException, "Invaild parameter i to get element ...");
        }
    }

    bool get(int i, T& e) const
    {
        bool ret = true;

        i = mod(i);

        ret = ((0 <= i) && (i < this->m_length));

        if( ret )
        {
            e = list_entry(position(i)->next, Node, head)->value;
        }

        return ret;
    }

    int find(const T& e) const
    {
        int ret = -1;
        int i = 0;
        list_head* slider = NULL;

        list_for_each(slider, &m_header)
        {
            if( list_entry(slider, Node, head)->value == e )
            {
                ret = i;
                break;
            }

            i++;
        }

        return ret;
    }

    int length() const
    {
        return this->m_length;
    }

    void clear()
    {
        while( this->m_length > 0 )
        {
            remove(0);
        }
    }

    bool move(int i, int step = 1)
    {
        bool ret = (step > 0);

        i = mod(i);

        ret = ret && ((0 <= i) && (i < this->m_length));

        if( ret )
        {
            m_current = position(i)->next;

            this->m_step = step;
        }

        return ret;
    }

    bool end()
    {
        return (m_current == NULL) || (this->m_length == 0);
    }

    T current()
    {
        if( !end() )
        {
            return list_entry(m_current, Node, head)->value;
        }
        else
        {
            THROW_EXCEPTION(INvalidOPerationException, "No value at current position ...");
        }
    }

    bool next()
    {
        int i = 0;

        while( (i < this->m_step) && !end() )
        {
            if( m_current != &m_header )
            {
                m_current  = m_current->next;
                i++;
            }
            else
            {
                m_current = m_current->next;
            }
        }

        if( m_current == &m_header )
        {
            m_current = m_current->next;
        }

        return (i == this->m_step);
    }

    bool pre()
    {
        int i =0;

        while( (i < this->m_step) && !end() )
        {
            if( m_current != &m_header )
            {
                m_current  = m_current->next;
                i++;
            }
            else
            {
                m_current = m_current->prev;
            }
        }

        if( m_current == &m_header )
        {
            m_current = m_current->next;
        }

        return (i == this->m_step);
    }

    ~DualCircleList()
    {
        clear();
    }
};

}

#endif // DUALCIRCLELIST_H

        下來我們寫點測試代碼看看上面的代碼有沒有問題


main.cpp 源碼

#include <iostream>
#include "DualCircleList.h"

using namespace std;
using namespace DTLib;

int main()
{
    DualCircleList<int> d1;

    for(int i=0; i<5; i++)
    {
        d1.insert(0, i);
        d1.insert(0, 5);
    }

    cout << "begin" << endl;

    d1.move(d1.length()-1);

    while( d1.find(5) != -1 )
    {
        if( d1.current() == 5 )
        {
            cout << d1.current() << endl;

            d1.remove(d1.find(d1.current()));
        }
        else
        {
            d1.pre();
        }
    }

    cout << "end" << endl;

    for(int i=0; i<d1.length(); i++)
    {
        cout << d1.get(i) << endl;
    }

    return 0;
}

        我們先來分析下,在插入 i 後,隨後便插入 5。先打印出 5 個 5,隨後刪除這 5 個數,然後在打印出剩下的 4 ~ 0。看看結果是否如我們所分析的那樣

圖片.png

        我們看到結果如我們所分析的那樣,證明我們的代碼寫的是沒問題的。通過今天對雙向循環鏈表的學習,總結如下:1、Linux 內核離那邊是帶頭結點的雙向循環鏈表;2、DualCircleList 使用 Linux 內核鏈表進行內部實現;3、DualCircleList 在循環遍歷時需要跳過頭結點;4、將 list_head 指針轉換爲目標結點指針時,使用 list_entry 宏。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章