日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

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

欄目分類
最近更新