網站首頁 編程語言 正文
雙向鏈表的基本操作的實現,供大家參考,具體內容如下
在之前的博客中介紹了三種鏈表,分別是單鏈表、單向循環鏈表以及雙向鏈表。本篇博客將用Python來實現雙向鏈表的如下操作。(用到的工具是Python 3)
is_empty() : 判斷鏈表是否為空
length() : 返回鏈表的長度
travel() : 遍歷
add(item) : 在頭部添加一個節點
append(item) : 在尾部添加一個節點
insert(pos, item) : 在指定位置 pos 添加一個節點
remove(item) : ?刪除一個節點
search(item) : ?查找節點是否存在
Python實現
class Node(object):
?? ?'''雙向鏈表節點'''
?? ?def __init__(self,item):
?? ??? ?self.item = item
?? ??? ?self.next = None
?? ??? ?self.prev = None
class DoubleLink(object):
?? ?'''雙向鏈表'''
?? ?def __init__(self):
?? ??? ?self._head = None
?? ?
?? ?def is_empty(self):
?? ??? ?'''判斷是否為空'''
?? ??? ?return self._head == None
?? ?
?? ?def length(self):
?? ??? ?'''返回鏈表的長度'''
?? ??? ?cur = self._head
?? ??? ?count = 0
?? ??? ?while cur!= None:
?? ??? ??? ?count += 1
?? ??? ??? ?cur = cur.next
?? ??? ?return count
?? ?
?? ?def travel(self):
?? ??? ?'''遍歷鏈表'''
?? ??? ?cur = self._head
?? ??? ?while cur != None:
?? ??? ??? ?print(cur.item)
?? ??? ??? ?cur = cur.next
?? ??? ?print("")
?? ?
?? ?def add(self, item):
?? ??? ?'''頭部插入元素'''
?? ??? ?node = Node(item)
?? ??? ?if self.is_empty():
?? ??? ??? ?# 如果是空鏈表,將_head指向None
?? ??? ??? ?self._head = node
?? ??? ?else:
?? ??? ??? ?# 將node的next指向_head的頭節點
?? ??? ??? ?node.next = self._head
?? ??? ??? ?# 將_head的頭節點的prev指向node
?? ??? ??? ?self._head.prev = node
?? ??? ??? ?# 將_head 指向node
?? ??? ??? ?self._head = node
?? ?
?? ?def append(self, item):
?? ??? ?'''尾部插入元素'''
?? ??? ?node = Node(item)
?? ??? ?if self.is_empty():
?? ??? ??? ?self._head = node
?? ??? ?else:
?? ??? ??? ?# 移動到鏈表尾部
?? ??? ??? ?cur = self._head
?? ??? ??? ?while cur.next != None:
?? ??? ??? ??? ?cur = cur.next
?? ??? ??? ?# 將尾結點cur的next指向node
?? ??? ??? ?cur.next = node
?? ??? ??? ?# 將node的prev指向cur
?? ??? ??? ?node.prev = cur
?? ?
?? ?def search(self, item):
?? ??? ?'''查找元素是否存在'''
?? ??? ?cur = self._head
?? ??? ?while cur != None:
?? ??? ??? ?if cur.item == item:
?? ??? ??? ??? ?return True
?? ??? ??? ?cur = cur.next
?? ??? ?return False
指定位置插入節點
在該操作中,要注意鏈的指向的先后順序。
def insert(self, pos, item):
?? ??? ?'''在指定位置添加節點'''
?? ??? ?if pos <= 0:
?? ??? ??? ?self.add(item)
?? ??? ?elif pos > (self.length() - 1):
?? ??? ??? ?self.append(item)
?? ??? ?else:
?? ??? ??? ?node = Node()
?? ??? ??? ?cur = self._head()
?? ??? ??? ?count = 0
?? ??? ??? ?# 移動到指定的前一個位置
?? ??? ??? ?while cur < pos - 1 :
?? ??? ??? ??? ?count += 1
?? ??? ??? ??? ?cur = cur.next
?? ??? ??? ?# 將node的prev指向cur
?? ??? ??? ?node.prev = cur
?? ??? ??? ?# 將node的next指向cur的下一個節點
?? ??? ??? ?node.next = cur.next
?? ??? ??? ?# 將cur的下一個節點的prev指向node
?? ??? ??? ?cur.next.prev = node
?? ??? ??? ?# 將cur.next指向node
?? ??? ??? ?cur.next = node
刪除元素
def remove(self, item):
?? ??? ?'''刪除元素'''
?? ??? ?if self.is_empty(): return?
?? ??? ?else:
?? ??? ??? ?cur = self._head
?? ??? ??? ?if cur.item == item:
?? ??? ??? ??? ?# 如果首節點的元素是要刪除的元素
?? ??? ??? ??? ?if cur.next == None:
?? ??? ??? ??? ??? ?# 如果鏈表中只有一個節點
?? ??? ??? ??? ??? ?self._head = None
?? ??? ??? ??? ?else:
?? ??? ??? ??? ??? ?cur.next.prev = None
?? ??? ??? ??? ??? ?self._head = cur.next
?? ??? ??? ??? ?return
?? ??? ??? ?while cur != None:
?? ??? ??? ??? ?if cur.item == item:
?? ??? ??? ??? ??? ?cur.prev.next = cur.next
?? ??? ??? ??? ??? ?cur.next.prev = cur.prev
?? ??? ??? ??? ??? ?break
?? ??? ??? ??? ?cur = cur.next
原文鏈接:https://blog.csdn.net/weixin_38746310/article/details/106598545
相關推薦
- 2022-09-17 Python利用AutoGrad實現自動計算函數斜率和梯度_python
- 2022-10-04 python中xml格式的轉換方法_python
- 2022-06-26 Go語言開源庫實現Onvif協議客戶端設備搜索_Golang
- 2022-09-16 C#數據庫操作的示例詳解_C#教程
- 2022-10-03 使用useImperativeHandle時父組件第一次沒拿到子組件的問題_React
- 2022-12-21 Redis?RDB與AOF持久化方式詳細講解_Redis
- 2022-06-16 zap接收gin框架默認的日志并配置日志歸檔示例_Golang
- 2023-12-15 log4j.properties自定義日志配置
- 最近更新
-
- 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同步修改后的遠程分支