栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++链表实现

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++链表实现

#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();
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/875873.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号