DualLinkList

1 单链表的缺陷

单链表具有单向性,只能从头结点开始高效访问链表中的数据元素。
缺陷: 如果需要逆向访问单链表中的数据元素将极其低效。

在这里插入图片描述


2 双向链表的实现

2.1 设计思路

在单链表的结点中增加一个指针pre,用于指向当前结点的前驱结点。

在这里插入图片描述

2.2 双向链表的继承层次结构

在这里插入图片描述

2.3 DualLinkList的定义

在这里插入图片描述

2.4 双向链表的特点

  • 双向链表是为了弥补单链表的缺陷而重新设计的。
  • 在概念上,双向链表不是单链表,没有继承关系。
  • 双向链表中的游标能够直接访问当前结点的前驱和后继。
  • 双向链表是线性表的最终实现(更贴近理论上的线性表)。

3 代码实现

DualLinkList.h

#ifndef DUALLINKLIST_H
#define DUALLINKLIST_H

#include "List.h"
#include "Exception.h"

namespace LemonLib
{
template < typename T >
class DualLinkList : public List<T>
{
protected:
    struct Node : public Object
    {
        T value;
        Node* next;
        Node* pre;
    };

    /* 注意:当前结构体必须继承自Object类,否则会导致和Node的内存布局不同,直接抛出异常 */
    mutable struct : public Object
    {
        char reserved[sizeof(T)];
        Node* next;
        Node* pre;
    }m_header;

    int m_length;
    Node* m_current;
    int m_step;

    Node* position(int index) const
    {
        Node* ret =  reinterpret_cast<Node*>(&m_header);

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

        return ret;
    }

    virtual Node* create()
    {
        return new Node();
    }

    virtual void destroy(Node* pn)
    {
        delete pn;
    }
public:
    DualLinkList()
    {
        m_header.next = NULL;
        m_header.pre = NULL;
        m_length = 0;
        m_current = NULL;
        m_step = 0;
    }

    bool insert(int index, const T& e)
    {
        bool ret = (0 <= index) && (index <= m_length);

        if (ret)
        {
            Node* node = create();

            if (node != NULL)
            {
                Node* current = position(index);
                Node* next = current->next;

                node->value = e;

                node->next = next; // 当前结点的next指针指向下一个结点
                current->next = node;

                if (current != reinterpret_cast<Node*>(&m_header))  // 当前插入的位置不是首结点
                {
                    node->pre = current;
                }
                else
                {
                    node->pre = NULL;
                }

                if (next != NULL)   // 当前插入的不是最后一个结点
                {
                    next->pre = node;
                }


                m_length++;
            }
            else
            {
                THROW_EXCEPTION(NoEnoughMemoryException, "No enough memory to inset element ...");
            }
        }

        return ret;
    }


    bool insert(const T& e)// 插入到尾部
    {
        return insert(m_length, e);
    }

    bool remove(int index)
    {
        bool ret = (0 <= index) && (index < m_length);

        if (ret)
        {
            Node* current = position(index);
            Node* toDel = current->next;
            Node* next = toDel->next;

            current->next = toDel->next;

            if (next != NULL)
            {
                next->pre = toDel->pre;
            }

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

            m_length--;
            destroy(toDel);
        }

        return ret;
    }

    bool set(int index, const T& e)
    {
        bool ret = (0 <= index) && (index < m_length);

        if (ret)
        {
            position(index)->next->value = e;
        }

        return ret;
    }

    bool get(int index, T& e) const
    {
        bool ret = (0 <= index) && (index < m_length);

        if (ret)
        {
            e = position(index)->next->value;
        }

        return ret;

    }

   virtual T get(int index) const
    {
        T ret;

        if (get(index, ret))
        {
            return ret;
        }
        else
        {
            THROW_EXCEPTION(InvalidParameterException, "Invalid parameter in get element...");
        }
    }

    int find(const T& e) const
    {
        int ret = -1;
        int index = 0;
        Node* current = m_header.next;

        while (current)
        {
            if (current->value == e)
            {
                ret = index;
                break;
            }
            else
            {
                index++;
                current = current->next;
            }


        }

        return ret;
    }

    virtual bool move(int index, int step = 1)
    {
        bool ret = ((0 <= index) && (index < m_length) && (step > 0));

        if (ret)
        {
            m_current = position(index)->next;
            m_step = step;
        }

        return ret;
    }

    virtual bool end()
    {
        return (m_current == NULL);
    }

    virtual T current()
    {
        if (!end())
        {
            return m_current->value;
        }
        else
        {
            THROW_EXCEPTION(InvalidOperationException, "Invalid operation to get current value ...");
        }
    }

    virtual bool next()
    {
        int i = 0;
        while ((i < m_step) && (!end()))
        {
            m_current = m_current->next;
            i++;
        }

        return (i == m_step);
    }

    virtual bool pre()
    {
        int i = 0;
        while ((i < m_step) && (!end()))
        {
            m_current = m_current->pre;
            i++;
        }

        return (i == m_step);
    }

    int length() const
    {
        return m_length;
    }

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

    ~DualLinkList()
    {
        clear();
    }
};
}

#endif // DUALLINKLIST_H

main.cpp

#include <iostream>

#include "SmartPointer.h"
#include "Exception.h"
#include "Object.h"
#include "List.h"
#include "SeqList.h"
#include "StaticList.h"
#include "DynamicList.h"
#include "Array.h"
#include "StaticArray.h"
#include "DynamicArray.h"
#include "LinkList.h"
#include "StaticLinkList.h"
#include "Pointer.h"
#include "SmartPointer.h"
#include "SharedPointer.h"
#include "CircleList.h"
#include "DualLinkList.h"

using namespace std;
using namespace LemonLib;



int main()
{
    DualLinkList<int> dl;

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

    int ret = -1;
    while ((ret = dl.find(8)) != -1)
    {
        dl.remove(ret);
    }

#if 0   // 这样删除的意义不大
    dl.move(dl.length()-1);
    while (!dl.end())
    {
        if (dl.current() == 8)
        {
            dl.remove(dl.find(8));
        }
        else
        {
            dl.pre();
        }
    }
#endif

#if 1
    for (dl.move(0); !dl.end(); dl.next())
    {
        cout << dl.current() << endl;
    }
#endif

#if 0
    for (dl.move(dl.length()-1); !dl.end(); dl.pre())
    {
        cout << dl.current() << endl;
    }
#endif


#if 0
    for (int i=0; i<5; i++)
    {
        cout << dl.get(i) << endl;
    }
#endif

    return 0;
}

4 开放性问题

DualLinkList和LinkList中存在很多完全一样的代码,如何进行重构降低代码的冗余性?冗余代码的出现是否意味着DualLinkList和LinkList之间应该是继承关系?

解决这个问题的方法就是DualLinkList继承LinkList,从而降低代码的冗余性。关于DualLinkList是否设计为LinkList的子类,这只是设计理念不同而已。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值