網站首頁 編程語言 正文
一、單向鏈表概念
單向鏈表的鏈接方向是單向的,由結點構成,head指針指向第一個成為head結點,而終止于最后一個指向None的指針,對鏈表的訪問要通過順序讀取從頭部開始。
二、建立節點對象
class Node: def __init__(self,data): self.data = data #節點的值域 self.next = None #連接下一個節點,暫時指向空
三、鏈表對象的初始定義
class linkList: def __init__(self): self.head = None #首先建立鏈表頭,暫時指向空
四、判斷鏈表是否為空
#判斷鏈表是否為空 def isEmpty(self): if self.head: return False else: return True
五、獲取鏈表長度
def length(self): if self.isEmpty(): return 0 else: t = self.head n = 1 while t.next: t = t.next n = n + 1 return n
六、向頭部添加節點
def addhead(self,data): node = Node(data) #新建一個節點 node.next = self.head #新建的節點接上原來的鏈表 self.head = node #重置鏈表的頭
七、向尾部添加節點
def addtail(self,data): node = Node(data) #新建一個節點 #先判斷鏈表是否為空 if self.isEmpty(): self.addhead(data) else: t = self.head while t.next: #通過循環找到尾部 t = t.next t.next = node #尾部接上
八、指定位置插入節點
def insert(self,data,index): if index == 0 or self.isEmpty(): self.addhead(data) elif index >= self.length(): self.addtail(data) else: node = Node(data) t = self.head n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = node node.next = a
九、刪除指定位置的節點
def delete(self,index): if self.isEmpty(): print("The linked list is empty") else: t = self.head if index == 0: self.head = t.next elif index == self.length() - 1: n = 1 while n < self.length() - 1: t = t.next n = n + 1 t.next = None elif index > self.length() - 1: print("Out of range") elif index < 0: print("Wrong operation") else: n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = a
十、查找是否有該數據的節點
def search(self,data): t = self.head n = 1 while t.next: if t.data == data: print(str(n) + " ") t = t.next n = n + 1 if (t.data == data): print(str(n) + " ")
十一、遍歷輸出整個鏈表
def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next: print(t.data) t = t.next print(t.data)
十二、輸入數據創建鏈表
def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next: print(t.data) t = t.next print(t.data)
十三、具體實現
data = input("input(以空格為界):") data = data.split(" ") linkList = linkList() linkList.form(data) #創建鏈表 addlist = linkList.addhead(5) #在頭節點加入 linkList.ergodic() #遍歷輸出 addlist = linkList.addtail(5) #在尾節點加入 linkList.ergodic() #遍歷輸出 linkList.search(5) #查找是否有"5"的節點 linkList.delete(4) #刪除第4個數據 linkList.ergodic() #遍歷輸出 print(linkList.length()) #輸出鏈表長度 linkList.insert(89,2) #指定位置插入數據 linkList.ergodic() #遍歷輸出
原文鏈接:https://blog.csdn.net/weixin_55073640/article/details/122736503
- 上一篇:Git常用命令介紹_其它綜合
- 下一篇:C#實現簡單的計算器小功能_C#教程
相關推薦
- 2023-05-15 shell參數換行與shell輸出換行的方法實例_linux shell
- 2022-06-30 Go實現分布式唯一ID的生成之雪花算法_Golang
- 2022-11-14 Go語言Goroutinue和管道效率詳解_Golang
- 2022-07-23 python實現雙向鏈表原理_python
- 2022-08-17 python可視化分析繪制帶趨勢線的散點圖和邊緣直方圖_python
- 2022-03-18 docker容器啟動設置固定IP的實現_docker
- 2022-11-13 Python?wheel文件詳細介紹_python
- 2023-03-27 react使用.env文件管理全局變量的方法_React
- 最近更新
-
- 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同步修改后的遠程分支