C++实现双向链表基本操作

// 双向链表
#if 1
#include <iostream>
using namespace std;

typedef int DataType;

class Node
{
public:
    DataType data;
    Node* next;
    Node* pri;
};

typedef Node* ListHead;

class DoubleList
{
public:
    DoubleList()
    {
        m_pHead = nullptr;
        //m_nLength = 0;
    }

    ListHead GetHead();
    uint32_t GetListLength();
    bool CreateList();
    bool InsertToHead(DataType value);
    bool InsertToTail(DataType value);
    bool InsertToAny(DataType value, uint32_t pos);

    bool DelFromHead();
    bool DelFromTail();
    bool DelFromAny(uint32_t pos);
    void Print();

private:
    //uint32_t m_nLength;
    ListHead m_pHead;
};

ListHead DoubleList::GetHead()
{
    return m_pHead;
}

void DoubleList::Print()
{
    if (m_pHead == nullptr)
    {
        return;
    }

    Node* pHead = m_pHead->next;
    while (pHead)
    {
        cout << pHead->data << ", ";
        pHead = pHead->next;
    }
    cout << endl;
}

bool DoubleList::CreateList()
{
    if (m_pHead == nullptr)  // 创建头结点
    {
        Node* pNode = new Node;
        if (pNode)
        {
            pNode->data = -1;
            pNode->next = nullptr;
            pNode->pri = nullptr;
            m_pHead = pNode;
            return true;
        }
    }
}

bool DoubleList::InsertToHead(DataType value)
{
    if (m_pHead == nullptr)
    {
        return false;
    }
    else if (m_pHead->next == nullptr)
    {
        Node* pNode = new Node;
        pNode->data = value;
        pNode->next = nullptr;
        pNode->pri = nullptr;

        m_pHead->next = pNode;
        pNode->pri = m_pHead;
    }
    else    
    {
        Node* pNode = new Node;
        pNode->data = value;
        pNode->next = nullptr;
        pNode->pri = nullptr;
        Node* pTmp = m_pHead->next;
        pNode->next = pTmp;
        pTmp->pri = pNode;
        m_pHead->next = pNode;
        pNode->pri = m_pHead;    
    }

    return true;
}

bool DoubleList::InsertToTail(DataType value)
{
    if (m_pHead == nullptr)
    {
        return false;
    }
    else if (m_pHead->next == nullptr)
    {
        Node* pNode = new Node;
        pNode->data = value;
        pNode->next = nullptr;
        pNode->pri = nullptr;

        m_pHead->next = pNode;
        pNode->pri = m_pHead;
    }
    else
    {
        Node* pHead = m_pHead->next;
        while (pHead->next)
        {
            pHead = pHead->next;
        }

        Node* pNode = new Node;
        pNode->data = value;
        pNode->next = nullptr;
        pNode->pri = nullptr;
        pHead->next = pNode;
        pNode->pri = pHead;
    }

    return true;
}

uint32_t DoubleList::GetListLength()
{
    if (m_pHead == nullptr)
    {
        return -1;
    }

    uint32_t uNum = 0;
    Node* pHead = m_pHead->next;
    while (pHead)
    {
        pHead = pHead->next;
        uNum++;
    }
    
    return uNum;
}

bool DoubleList::InsertToAny(DataType value, uint32_t pos) // pos指的是第几个节点插入, 默认从1开始
{
    if (m_pHead == nullptr || pos <= 0 || pos > GetListLength())
    {
        return false;
    }
    else if (m_pHead->next == nullptr && pos == 1)
    {
        if (!InsertToHead(value))
        {
            return false;
        }
    }
    else
    {
        if (m_pHead->next != nullptr)
        {
            Node* pHead = m_pHead->next;

            while (pos > 0)
            {
                pos--;
                pHead = pHead->next;
            }

            if (!pHead)
            {
                if (!InsertToTail(value))
                {
                    return false;
                }
            }
            else
            {
                Node* pNode = new Node;
                pNode->data = value;
                pNode->next = nullptr;
                pNode->pri = nullptr;

                pNode->pri = pHead->pri;
                pNode->next = pHead;
                pHead->pri->next = pNode;
                pHead->pri = pNode;
            }
        }
    }
    return true;
}

bool DoubleList::DelFromHead()
{
    if (m_pHead == nullptr || m_pHead->next == nullptr)
    {
        return false;
    }
    else
    {
        Node* pHead = m_pHead->next;
        m_pHead->next = pHead->next;
        pHead->next->pri = m_pHead;

        pHead->next = nullptr;
        pHead->pri = nullptr;
        delete pHead;
        pHead = nullptr;
    }

    return true;
}

bool DoubleList::DelFromTail()
{
    if (m_pHead == nullptr || m_pHead->next == nullptr)
    {
        return false;
    }
    else
    {
        Node* pHead = m_pHead->next;
        while (pHead->next)
        {
            pHead = pHead->next;
        }

        pHead->pri->next = nullptr;
        pHead->next = nullptr;
        pHead->pri = nullptr;
        delete pHead;
        pHead = nullptr;
    }

    return true;
}

bool DoubleList::DelFromAny(uint32_t pos)
{
    if (m_pHead == nullptr || m_pHead->next == nullptr || pos <= 0 || pos >= GetListLength())
    {
        return false;
    }
    else if (pos = GetListLength() - 1)
    {
        if (!DelFromTail())
        {
            return false;
        }
    }
    else
    {
        if (m_pHead->next != nullptr)
        {
            Node* pHead = m_pHead->next;

            while (pos > 0)
            {
                pos--;
                pHead = pHead->next;
            }

            pHead->pri->next = pHead->next;
            pHead->next->pri = pHead->pri;

            pHead->next = nullptr;
            pHead->pri = nullptr;
            delete pHead;
            pHead = nullptr;
        }
    }
    return true;
}

int main()
{
    DoubleList d;
    d.CreateList();
    d.InsertToHead(1);
    d.InsertToHead(2);
    d.InsertToHead(3);
    d.InsertToTail(4);
    d.InsertToTail(5);
    d.InsertToAny(6, 4);
    d.Print();
    //d.DelFromHead();
    //d.DelFromTail();
    d.DelFromAny(4);
    d.Print();
    return 0;
}
#endif

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值