Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實(shí)例詳解【單鏈表、循環(huán)鏈表】
本文實(shí)例講述了Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法。分享給大家供大家參考,具體如下:
本文將為大家講解:
(1)從鏈表節(jié)點(diǎn)的定義開始,以類的方式,面向?qū)ο蟮乃枷脒M(jìn)行鏈表的設(shè)計(jì)
(2)鏈表類插入和刪除等成員函數(shù)實(shí)現(xiàn)時(shí)需要考慮的邊界條件,
prepend(頭部插入)、pop(頭部刪除)、append(尾部插入)、pop_last(尾部刪除)
2.1 插入:
空鏈表
鏈表長(zhǎng)度為1
插入到末尾
2.2 刪除
空鏈表
鏈表長(zhǎng)度為1
刪除末尾元素
(3)從單鏈表到單鏈表的一眾變體:
帶尾節(jié)點(diǎn)的單鏈表
循環(huán)單鏈表
雙鏈表
1. 鏈表節(jié)點(diǎn)的定義
class LNode: def __init__(self, elem, next_=None): self.elem = elem self.next = next_
2. 單鏈表的實(shí)現(xiàn)
重點(diǎn)理解插入、刪除的實(shí)現(xiàn)及其需要考慮的邊界條件:
class LinkedListUnderflow(ValueError):
pass
class LList:
def __init__(self):
self._head = None
def is_empty(self):
return self._head is None
def prepend(self, elem):
self._head = LNode(elem, self._head)
def pop(self):
if self._head is None:
raise LinkedListUnderflow('in pop')
e = self._head.elem
self._head = self._head.next
return e
def append(self, elem):
if self._head is None:
self._head = LNode(elem)
return
p = self._head
while p.next is not None:
p = p.next
p.next = LNode(elem)
def pop_last(self):
if self._head is None:
raise LinkedListUnderflow('in pop_last')
p = self._head
if p.next is None:
e = p.elem
self._head = None
return e
while p.next.next is not None:
p = p.next
e = p.next.elem
p.next = None
return e
簡(jiǎn)單總結(jié):
(0)能夠訪問 p.next.next 的前提是 p.next 不為空;
(1)尾部插入,如果鏈表不為空,需且僅需改變的是尾部節(jié)點(diǎn)的指針;
(2)尾部刪除,如果鏈表長(zhǎng)度不為空,需且僅需改變的是倒數(shù)第二個(gè)節(jié)點(diǎn)的指針。
單鏈表的簡(jiǎn)單變形:具有尾部節(jié)點(diǎn)的單鏈表
class LList1(LList): def __init__(self): LList.__init__(self) self._rear = None ...
我們僅需重寫的是:頭部的插入、尾部的插入、尾部的刪除
def prepend(self, elem):
if self._head is None:
self._head = LNode(elem)
self._rear = self._head
else:
self._head = LNode(elem, self._head)
def append(self, elem):
if self._head is None:
self._head = LNode(elem)
self._rear = self._head
else:
self._rear.next = LNode(elem)
self._rear = self._rear.next
def pop_last(self):
if self._head is None:
raise LinkedListUnderflow('in pop_last')
p = self._head
if p.next is None:
e = p.elem
self._head = None
return e
while p.next.next is not None:
p = p.next
e = p.next.elem
self._rear = p
p.next = None
return e
單鏈表的變體:循環(huán)單鏈表
class LCList:
def __init__(self):
self._rear = None
def prepend(self, elem):
if self._rear is None:
self._rear = LNode(elem)
self._rear.next = self._rear
else:
self._rear.next = LNode(elem, self._rear.next)
def append(self, elem):
self.prepend(elem)
self_rear = self._rear.next
def pop(self):
if self._rear is None:
raise LinkedListUnderflow('in pop')
p = self._rear.next
if p is None:
self._rear = None
else:
self._rear.next = p.next
return p.elem
def printall(self):
if self._rear is None:
raise ...
p = self._rear.next
while True:
print(p.elem)
if p is self._rear:
break
p = p.next
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python單向循環(huán)鏈表實(shí)例詳解
- Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
- python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- python/golang實(shí)現(xiàn)循環(huán)鏈表的示例代碼
- python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例
- Python雙向循環(huán)鏈表實(shí)現(xiàn)方法分析
- Python實(shí)現(xiàn)的單向循環(huán)鏈表功能示例
- python雙向鏈表實(shí)例詳解
- Python實(shí)現(xiàn)雙向鏈表基本操作
- python雙向循環(huán)鏈表實(shí)例詳解
相關(guān)文章
Python3+selenium配置常見報(bào)錯(cuò)解決方案
這篇文章主要介紹了Python3+selenium配置常見報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
利用For循環(huán)遍歷Python字典的三種方法實(shí)例
字典由多個(gè)鍵和其對(duì)應(yīng)的值構(gòu)成的鍵—值對(duì)組成,鍵和值中間以冒號(hào):隔開,項(xiàng)之間用逗號(hào)隔開,整個(gè)字典是由大括號(hào){}括起來的,下面這篇文章主要給大家介紹了關(guān)于如何利用For循環(huán)遍歷Python字典的三種方法,需要的朋友可以參考下2022-03-03
基于Python實(shí)現(xiàn)GeoServer矢量文件批量發(fā)布
由于矢量圖層文件較多,手動(dòng)發(fā)布費(fèi)時(shí)費(fèi)力,python支持的關(guān)于geoserver包又由于年久失修,無法在較新的geoserver版本中正常使用。本文為大家準(zhǔn)備了Python自動(dòng)化發(fā)布矢量文件的代碼,需要的可以參考一下2022-07-07
用Python爬取英雄聯(lián)盟的皮膚詳細(xì)示例
大家好,本篇文章主要講的是用Python爬取英雄聯(lián)盟的皮膚詳細(xì)示例,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Python中OTSU算法的原理與實(shí)現(xiàn)詳解
OTSU算法是大津展之提出的閾值分割方法,又叫最大類間方差法,本文主要為大家詳細(xì)介紹了OTSU算法的原理與Python實(shí)現(xiàn),感興趣的小伙伴可以了解下2023-12-12
python中numpy基礎(chǔ)學(xué)習(xí)及進(jìn)行數(shù)組和矢量計(jì)算
這篇文章主要給大家介紹了python中numpy基礎(chǔ)知識(shí),以及進(jìn)行數(shù)組和矢量計(jì)算的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02

