c++链表实现

c++链表实现,第1张

#include

using namespace std;

template
struct  Node
{
    T  Value;
    Node* NextNode;
};

template
class LinkedList
{
public:
    Node* List;
    //创建链表
    LinkedList(unsigned int Count)
    {
        Node* pHeader = (Node*)malloc(sizeof(Node));
        if (pHeader == nullptr)
        {
            List = nullptr;
            return;
        }
        pHeader->Value = 445145145;
        pHeader->NextNode = nullptr;
        Node* pTail = pHeader;
        for (unsigned int i = 0; i < Count - 1; i++)
        {
            Node* pNode = (Node*)malloc(sizeof(Node));
            if (pNode != nullptr)
            {
                pNode->Value = 0;
                pNode->NextNode = nullptr;
                pTail->NextNode = pNode;
                pTail = pNode;
            }
        }
        List = pHeader;
        return;
    }
    //获取长度
    unsigned int GetLength()
    {
        Node* p = List;
        int Count = 0;
        do
        {
            Count++;
            p = p->NextNode;
        }while (p != nullptr);
        return Count;
    }
    //获取索引对应节点指针
    Node* GetNodePointer(unsigned int Index)
    {
        Node* p = List;
        for (unsigned int i = 0; i < Index; i++)
        {
            p = p->NextNode;
        }
        return p;
    }
    //获取索引对应节点值
    T GetNodeValue(unsigned int Index)
    {
        if (Index >= GetLength()) return 0;
        Node* p = List;
        for (unsigned int i = 0; i < Index; i++)
        {
            p = p->NextNode;
        }
        return p->Value;
    }
    //遍历链表
    void Traverse()
    {
        Node* p = List;
        do
        {
            cout << p->Value << endl;
            p = p->NextNode;
        } while (p != nullptr);
    }
    //插入值
    void Insert(unsigned int Index, T Value)
    {
        if (Index >= GetLength()) return;
        Node* AddedNode = (Node*)malloc(sizeof(Node));
        if (AddedNode != nullptr)
        {
            AddedNode->Value = Value;
            Node* p = GetNodePointer(Index - 1);
            Node* q = GetNodePointer(Index);
            p->NextNode = AddedNode;
            AddedNode->NextNode = q;
        }
    }
    //向链表最后插入值
    void Push_Back(T Value)
    {
        Node* AddedNode = (Node*)malloc(sizeof(Node));
        if (AddedNode != nullptr)
        {
            AddedNode->Value = Value;
            AddedNode->NextNode = nullptr;
            Node* p = GetNodePointer(GetLength() - 1);
            p->NextNode = AddedNode;
        }
    }
    //删除链表最后的节点
    void Pop_Back()
    {
        Node* p = GetNodePointer(GetLength() - 2);
        free(p->NextNode);
        p->NextNode = nullptr;
    }
    //删除链表特定索引对应节点
    void Delete(unsigned int Index)
    {
        if (Index == GetLength() - 1)
        {
            cout << "e" << endl;
            Node* p = GetNodePointer(Index - 1);
            cout << p->NextNode << endl;
            p->NextNode = nullptr;
            free(p->NextNode);
        }
        else
        {
            Node* p = GetNodePointer(Index - 1);
            Node* Del = p->NextNode;
            Node* q = p->NextNode->NextNode;
            p->NextNode = q;
            free(Del);
        }
    }
    //删除链表
    void Dispose()
    {
        Node* p = List;
        Node* q = p;
        while (p->NextNode != nullptr)
        {
            p = p->NextNode;
            free(q);
            q = p->NextNode;
        }
    }
};


int main()
{
    //创建链表
    LinkedList List(3);
    //初始化链表
    for (int i = 0; i < 3; i++)
    {
        Node* p = List.GetNodePointer(i);
        p->Value = i * 123;
    }
    List.Traverse();
    cout << "---" << endl;

    List.Push_Back(114514);
    List.Push_Back(114513);
    List.Insert(4, 40);
    List.Traverse();
    cout << "---" << endl;

    List.Pop_Back();
    List.Traverse();
    cout << "---" << endl;

    cout << List.GetNodeValue(4) << endl;
    cout << "---" << endl;

    List.Dispose();
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/915014.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-16
下一篇2022-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存