学无先后,达者为师

网站首页 编程语言 正文

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

作者:ctrl A_ctrl C_ctrl V   更新时间: 2022-05-25 编程语言

 链表有两种:

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

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

直接上代码:

#include 
#include 
#include 
#include 
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;ival=i*i;    //为节点赋值
            node->next=nullptr;
            head->next=node;   //head指向下一个节点(即当前节点)
            head=node;     //将当前节点设为head
        }
        cout<<"Create a new ListNode with len of "<val<<'\t';
                head=head->next;
            }
        cout< node;
            while(head!=nullptr)
            {
                node.push_back(head->val);
                head=head->next;
            }
            while(!node.empty())
            {
                //先输出node中的最后一个元素,再删除最后一个元素。而不是先对node做reverse再正向输出。
                cout<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"<

程序输出:

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

原文链接:https://blog.csdn.net/qq_43799400/article/details/122941144

栏目分类
最近更新