網站首頁 編程語言 正文
?鏈表有兩種:
- ?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;i val=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
相關推薦
- 2022-04-08 go實現圖片拼接與文字書寫的方法實例_Golang
- 2022-10-14 yum 倉庫管理 yum-config-manager
- 2023-01-11 解讀時間序列分析之ADF檢驗_python
- 2023-04-12 Blazor實現組件嵌套傳遞值的示例詳解_其它綜合
- 2022-10-03 Python實現數據清洗的示例詳解_python
- 2023-02-12 Python利用物理引擎Pymunk編寫一個解壓小游戲_python
- 2022-05-18 python基礎教程之csv格式文件的寫入與讀取_python
- 2022-06-13 云計算openstack框架分類及發展階段概述_OpenStack
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支