網站首頁 編程語言 正文
一、本章重點
- 無頭單向非循環鏈表介紹
- 無頭單向非循環鏈表常用接口實現
- 在線oj訓練
二、鏈表介紹
概念:鏈表是一種物理存儲結構上非連續、非順序的存儲結構,數據元素的邏輯順序是通過鏈表 中的指針鏈接次序實現的。
簡單的說:鏈表就是一些結構體相互關聯起來,而關聯的手段就是指針,通過存儲下一個結構體的地址,就能挨個訪問存在結構體里的數據。
相比于順序表來說。鏈表能夠解決順序表的一些缺點。
- 從內存角度來說:無論是靜態順序表還是動態順序表都會有一定的內存浪費,鏈表則是用一個節點申請一個節點,無內存浪費。
- 從效率角度上來說:順序表不管是頭插還是中間插入與刪除都要移動數據,時間復雜度是O(N)。而鏈表則只要改變指針指向即可達到插入和刪除的效果。
實際中要實現的鏈表的結構非常多樣,以下情況組合起來就有8種鏈表結構:
1. 單向、雙向
2. 帶頭、不帶頭
3. 循環、非循環
帶頭:
存在一個哨兵位的節點,該節點不存儲任何有效數據,屬于無效節點,但通過這個無效節點當頭節點讓我們在某些方面使用會有一些優勢。
比如,你頭插新節點時,不需要傳二級指針,因為我們的頭節點始終指向這個無效節點。
帶頭單向非循環鏈表
雙向:
指的是節點中不再只有一個指針,而是有兩個指針,一個指向前一個節點,另一個指向后一個節點。
無頭雙向非循環鏈表
循環:
尾指針不再指向NULL,而是指向頭節點。?
無頭單向循環鏈表
三、無頭單向非循環鏈表常用接口實現
3.1動態申請一個節點
SLTNode* BuySListNode(SLDataType x) { SLTNode* newnode = malloc(sizeof(SLTNode)); if (newnode == NULL) { printf("malloc fail\n"); exit(-1); } newnode->data = x; newnode->next = NULL; return newnode; }
3.2單鏈表打印
void SListPrint(SLTNode* phead) { SLTNode* cur = phead; while (cur) { printf("%d->", cur->data); cur = cur->next; } printf("NULL\n"); }
3.3單鏈表尾插
void SListPushBack(SLTNode** pphead, SLDataType x) { if (*pphead==NULL) { *pphead = BuySListNode(x); return; } else { SLTNode* tail; tail = *pphead; while (tail->next) { tail = tail->next; } tail->next = BuySListNode(x); return; } }
3.4單鏈表的頭插
void SListPushFront(SLTNode** pphead, SLDataType x) { SLTNode* newnode = BuySListNode(x); newnode->next = *pphead; *pphead = newnode; }
3.5單鏈表的尾刪
void SListPopBack(SLTNode** pphead) { if (*pphead == NULL) { return; } else if((*pphead)->next==NULL) { *pphead = NULL; } else { SLTNode* prev = NULL; SLTNode* tail = *pphead; while (tail->next) { prev = tail; tail = tail->next; } free(tail); prev->next = NULL; } }
3.6單鏈表頭刪
void SListPopFront(SLTNode** pphead) { if (*pphead == NULL) { return; } else { SLTNode* temp = *pphead; (*pphead) = (*pphead)->next; free(temp); } }
3.7單鏈表查找
SLTNode* SListSearch(SLTNode* phead, SLDataType x) { while (phead) { if (phead->data == x) { return phead; } phead = phead->next; } return NULL; }
3.8單鏈表在pos位置之前插入x
void SListInsert(SLTNode** pphead, SLTNode* pos, SLDataType x) { SLTNode* newnode = BuySListNode(x); if (*pphead == NULL) { return; } else if(*pphead==pos) { newnode->next = pos; *pphead = newnode; } else { SLTNode* cur = *pphead; while (cur->next != pos) { cur = cur->next; } newnode->next = pos; cur->next = newnode; } }
3.9單鏈表刪除pos位置的節點
void SListErase(SLTNode** phead, SLTNode* pos) { if (*phead == NULL) { return; } else if (*phead == pos) { *phead == NULL; } else { SLTNode* cur = *phead; while (cur->next != pos) { cur = cur->next; } cur->next = pos->next; free(pos); } }
四、在線oj訓練
4.1移除鏈表元素(力扣)
給你一個鏈表的頭節點?head
?和一個整數?val
?,請你刪除鏈表中所有滿足?Node.val == val
?的節點,并返回?新的頭節點?。
輸入:head = [1,2,6,3,4,5,6], val = 6
輸出:[1,2,3,4,5]
輸入:head = [7,7,7,7], val = 7
輸出:[]
?思路:
前后指針(跟班指針),初始轉態prev指向NULL,cur指向head,如果出現cur->val==val.
就進行刪除,刪除步驟為prev->next==cur->next;cur=cur->next。迭代過程為:prev=cur,cur==cur->next。
特殊情況,如果要刪除的val為頭節點,則刪除步驟為head=cur->next;free(cur);cur=head。
代碼參考:
typedef struct ListNode ListNode; struct ListNode* removeElements(struct ListNode* head, int val) { ListNode* cur=head; ListNode* prev=NULL; while(cur) { if(cur->val==val) { if(cur==head) { head=cur->next; free(cur); cur=head; } else { prev->next=cur->next; cur=cur->next; } } else { prev=cur; cur=cur->next; } } return head; }
4.2反轉單鏈表(力扣)
給你單鏈表的頭節點?head
?,請你反轉鏈表,并返回反轉后的鏈表。
輸入:head = [1,2,3,4,5]
輸出:[5,4,3,2,1]
輸入:head = []
輸出:[]
這里提供一個頭插思路:newhead=NULL,將head中的數據從左到右依次頭插至newhead鏈表中。
typedef struct ListNode ListNode; struct ListNode* reverseList(struct ListNode* head) { ListNode* newhead=NULL; ListNode* cur=head; if(cur==NULL) { return cur; } ListNode* next=cur->next; while(cur) { cur->next=newhead; newhead=cur; cur=next; if(next) next=next->next; } return newhead; }
注意:結束條件是cur==NULL;此時如果next往后走,next=next->next;會出現NULL解引用的問題。
其次要考慮cur==NULL的問題,不然ListNode* next=cur->next也是對空指針解引用。
其實可以化簡為:
struct ListNode* reverseList(struct ListNode* head) { struct ListNode* cur = head; struct ListNode* newhead = NULL; while(cur) { struct ListNode* next = cur->next; cur->next = newhead; newhead = cur; cur = next; } return newhead; }
原文鏈接:https://blog.csdn.net/m0_62171658/article/details/123300104
相關推薦
- 2022-07-08 Python數據分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖_python
- 2022-07-19 tomcat升級 遇到的坑 運行tomcat的時候出現NosuchMethodError
- 2022-11-04 ASP.NET?MVC實現登錄后跳轉到原界面_實用技巧
- 2022-10-19 Go?熱加載之fresh詳解_Golang
- 2022-06-08 Spring Cloud Nacos 配置變更感知
- 2022-12-05 Golang中的錯誤處理的示例詳解_Golang
- 2022-06-21 Python+Django實現簡單HelloWord網頁的示例代碼_python
- 2023-04-26 C語言函數聲明以及函數原型超詳細講解示例_C 語言
- 最近更新
-
- 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同步修改后的遠程分支