網(wǎng)站首頁 編程語言 正文
一、單向鏈表概念
單向鏈表的鏈接方向是單向的,由結(jié)點構(gòu)成,head指針指向第一個成為head結(jié)點,而終止于最后一個指向None的指針,對鏈表的訪問要通過順序讀取從頭部開始。
二、建立節(jié)點對象
class Node: def __init__(self,data): self.data = data #節(jié)點的值域 self.next = None #連接下一個節(jié)點,暫時指向空
三、鏈表對象的初始定義
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
六、向頭部添加節(jié)點
def addhead(self,data): node = Node(data) #新建一個節(jié)點 node.next = self.head #新建的節(jié)點接上原來的鏈表 self.head = node #重置鏈表的頭
七、向尾部添加節(jié)點
def addtail(self,data): node = Node(data) #新建一個節(jié)點 #先判斷鏈表是否為空 if self.isEmpty(): self.addhead(data) else: t = self.head while t.next: #通過循環(huán)找到尾部 t = t.next t.next = node #尾部接上
八、指定位置插入節(jié)點
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
九、刪除指定位置的節(jié)點
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
十、查找是否有該數(shù)據(jù)的節(jié)點
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)
十二、輸入數(shù)據(jù)創(chuàng)建鏈表
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)
十三、具體實現(xiàn)
data = input("input(以空格為界):") data = data.split(" ") linkList = linkList() linkList.form(data) #創(chuàng)建鏈表 addlist = linkList.addhead(5) #在頭節(jié)點加入 linkList.ergodic() #遍歷輸出 addlist = linkList.addtail(5) #在尾節(jié)點加入 linkList.ergodic() #遍歷輸出 linkList.search(5) #查找是否有"5"的節(jié)點 linkList.delete(4) #刪除第4個數(shù)據(jù) linkList.ergodic() #遍歷輸出 print(linkList.length()) #輸出鏈表長度 linkList.insert(89,2) #指定位置插入數(shù)據(jù) linkList.ergodic() #遍歷輸出
原文鏈接:https://blog.csdn.net/weixin_55073640/article/details/122736503
相關(guān)推薦
- 2022-03-26 C++中靜態(tài)數(shù)據(jù)成員使用示例_C 語言
- 2023-04-17 深入理解Django的信號機(jī)制_python
- 2022-09-30 centos7安裝docker容器的超詳細(xì)步驟記錄_docker
- 2023-05-11 oracle中如何刪除億級數(shù)據(jù)_oracle
- 2024-01-07 Plugin ‘org.springframework.boot:spring-boot-maven
- 2023-03-20 C#?獲取XML文件內(nèi)容的多種方式總結(jié)_C#教程
- 2021-11-30 Linux?Autofs自動掛載服務(wù)安裝部署教程_Linux
- 2022-06-23 VBS?批量Ping的項目實現(xiàn)_vbs
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支