iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >C++ 自定义单向链表 ListNode详情
  • 658
分享到

C++ 自定义单向链表 ListNode详情

2024-04-02 19:04:59 658人浏览 薄情痞子
摘要

 链表有两种:  1、带头结点,头结点存放的是链表的长度,从第二个节点开始存放数据。 2、不带头结点,没有存放链表长度的节点,从头结点开始就存放数据。

 链表有两种:

  •  1、带头结点,头结点存放的是链表的长度,从第二个节点开始存放数据。
  •  2、不带头结点,没有存放链表长度的节点,从头结点开始就存放数据。

小编这里定义的链表是第二种。

直接上代码:

#include <iOStream>
#include <assert.h>
#include <vector>
#include <alGorithm>
using namespace std;

struct Listnode
{
    int val;   //当前节点的值
    ListNode *next;   //指向下一个节点的指针
    ListNode() : val(0), next(nullptr) {}   //初始化当前结点值为默认值0,指针为空
    ListNode(int x) : val(x), next(nullptr) {}    //初始化当前结点值为x,指针为空
    ListNode(int x, ListNode *next) : val(x), next(next) {}    //初始化当前结点值为x,下一个绩点为next
};


class Solution
{
public:
    //创建长度为len的单向链表
    void createList(ListNode *head,int len){
        for(int i=1;i<len;i++)   //len-1个节点,加上head节点共len个
        {
            ListNode *node=new ListNode;   //每次都需要实例化一个ListNode
            node->val=i*i;    //为节点赋值
            node->next=nullptr;
            head->next=node;   //head指向下一个节点(即当前节点)
            head=node;     //将当前节点设为head
        }
        cout<<"Create a new ListNode with len of "<<len<<" successfully."<<endl;
    }

    //打印链表(顺序)
    void printList(ListNode *head){
        if(head==nullptr)        
            cout<<"empty list."<<endl;
        else
            while(head!=nullptr)
            {
                cout<<head->val<<'\t';
                head=head->next;
            }
        cout<<endl;
    }

    //打印链表(逆序)
    void reversePrintList(ListNode *head){
        //因为ListNode只能根据next单向索引,无法逆向回溯,所以只能将节点数值存在vector中反向输出。
        //目前只想到了这种方法。
        if(head==nullptr)
        {
            cout<<"empty list."<<endl;
            exit(1);
        }
        else
        {
            vector<int> node;
            while(head!=nullptr)
            {
                node.push_back(head->val);
                head=head->next;
            }
            while(!node.empty())
            {
                //先输出node中的最后一个元素,再删除最后一个元素。而不是先对node做reverse再正向输出。
                cout<<node.back()<<'\t';
                node.pop_back();
            }
            cout<<endl;
        }
    }

    //在链表尾节点添加一个新节点
    void pushBack(ListNode *head,int val){
        ListNode *node=new ListNode(val,nullptr);   //要添加的新节点
        if(head==nullptr)
            head=node;
        else
        {
            while(head->next!=nullptr)    //while循环结束后head就是尾结点了
                head=head->next;
            head->next=node;
        }
    }

    //更改链表尾节点数值
    void changeBackValue(ListNode *head,int val){
        assert(head!=nullptr);
        while(head->next!=nullptr)    //while循环结束后head就是尾结点了
            head=head->next;
        head->val=val;
    }

    //删除链表尾节点
    void popBack(ListNode *head){
        assert(head!=nullptr);
        while(head->next->next!=nullptr)   //while循环结束后head是倒数第二个节点,其next指向尾节点
            head=head->next;
        head->next=nullptr;   //删除尾节点
        //注意不要直接delete尾结点,因为尾结点的next是nullptr,直接delete nullptr会输出很多乱码。
    }

    //删除链表中节点值等于指定值的节点(不包括头节点)
    void deleteNode(ListNode *head, int val) {
        assert(head != nullptr);
        ListNode *node = head;    //copy一份链表
        while (head->next != nullptr)
        {
            if (head->next->val == val)
                node->next=head->next->next;
            head=head->next;
            node=node->next;
        }
    }

    //清空列表
    void clearList(ListNode *head){
        head->next=nullptr;   //清楚头结点之后的所有节点
        //清空列表的功能一直不知道怎么实现,头结点不知道怎么删除。
    }
};


int main()
{
    Solution solution;
    ListNode *listnode=new ListNode(5,nullptr);   //初始化链表的head节点
    solution.printList(listnode);           // 5
    solution.createList(listnode,5);   
    solution.printList(listnode);           // 5       1       4       9       16
    solution.pushBack(listnode,30);
    solution.printList(listnode);           // 5       1       4       9       16      30
    solution.reversePrintList(listnode);    // 30      16      9       4       1       5
    solution.changeBackValue(listnode,88);
    solution.printList(listnode);           // 5       1       4       9       16      88
    solution.popBack(listnode);
    solution.printList(listnode);           // 5       1       4       9       16
    solution.pushBack(listnode,101);
    solution.printList(listnode);           // 5       1       4       9       16      101
    solution.deleteNode(listnode,9);
    solution.printList(listnode);           // 5       1       4       16      101
    solution.clearList(listnode);
    solution.printList(listnode);           // 5
    cout<<"END"<<endl;
    return 0;
}

程序输出:

5
Create a new ListNode with len of 5 successfully.
5       1       4       9       16
5       1       4       9       16      30
30      16      9       4       1       5
5       1       4       9       16      88
5       1       4       9       16
5       1       4       9       16      101
5       1       4       16      101
5
END

到此这篇关于c++ 自定义单向链表 ListNode详情的文章就介绍到这了,更多相关C++ 自定义单向链表内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: C++ 自定义单向链表 ListNode详情

本文链接: https://www.lsjlt.com/news/143525.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作