
Python數(shù)據(jù)結(jié)構(gòu)之雙向鏈表的定義與使用方法示例
本文實(shí)例講述了Python數(shù)據(jù)結(jié)構(gòu)之雙向鏈表的定義與使用方法。分享給大家供大家參考,具體如下:
和單鏈表類似,只不過是增加了一個(gè)指向前面一個(gè)元素的指針而已。
示意圖:
python 實(shí)現(xiàn)代碼:
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Node(object):
def __init__(self,val,p=0):
self.data = val
self.next = p
self.prev = p
class LinkList(object):
def __init__(self):
self.head = 0
def __getitem__(self, key):
if self.is_empty():
print 'linklist is empty.'
return
elif key <0 or key > self.getlength():
print 'the given key is error'
return
else:
return self.getitem(key)
def __setitem__(self, key, value):
if self.is_empty():
print 'linklist is empty.'
return
elif key <0 or key > self.getlength():
print 'the given key is error'
return
else:
self.delete(key)
return self.insert(key)
def initlist(self,data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
node = Node(i)
p.next = node
node.prev = p
p = p.next
def getlength(self):
p = self.head
length = 0
while p!=0:
length+=1
p = p.next
return length
def is_empty(self):
if self.getlength() ==0:
return True
else:
return False
def clear(self):
self.head = 0
def append(self,item):
q = Node(item)
if self.head ==0:
self.head = q
else:
p = self.head
while p.next!=0:
p = p.next
p.next = q
q.prev = p
def getitem(self,index):
if self.is_empty():
print 'Linklist is empty.'
return
j = 0
p = self.head
while p.next!=0 and j <index:
p = p.next
j+=1
if j ==index:
return p.data
else:
print 'target is not exist!'
def insert(self,index,item):
if self.is_empty() or index<0 or index >self.getlength():
print 'Linklist is empty.'
return
if index ==0:
q = Node(item,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
q = Node(item,p)
post.next = q
q.prev = post
q.next = p
p.prev = q
def delete(self,index):
if self.is_empty() or index<0 or index >self.getlength():
print 'Linklist is empty.'
return
if index ==0:
q = Node(item,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=0 and j<index:
post = p
p = p.next
j+=1
if index ==j:
post.next = p.next
p.next.prev = post
def index(self,value):
if self.is_empty():
print 'Linklist is empty.'
return
p = self.head
i = 0
while p.next!=0 and not p.data ==value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1
l = LinkList()
l.initlist([1,2,3,4,5])
print "腳本之家測(cè)試結(jié)果:"
print l.getitem(4)
l.append(6)
print l.getitem(5)
l.insert(4,40)
print l.getitem(3)
print l.getitem(4)
print l.getitem(5)
l.delete(5)
print l.getitem(5)
l.index(5)
結(jié)果為;
和單鏈表結(jié)果一樣。
PS:雙向鏈表就是將鏈表首尾相接。
數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼
若不方便掃碼,搜微信號(hào):CDAshujufenxi
DSGE 模型中的 Et:理性預(yù)期算子的內(nèi)涵、作用與應(yīng)用解析 動(dòng)態(tài)隨機(jī)一般均衡(Dynamic Stochastic General Equilibrium, DSGE)模 ...
2025-09-17Python 提取 TIF 中地名的完整指南 一、先明確:TIF 中的地名有哪兩種存在形式? 在開始提取前,需先判斷 TIF 文件的類型 —— ...
2025-09-17CDA 數(shù)據(jù)分析師:解鎖表結(jié)構(gòu)數(shù)據(jù)特征價(jià)值的專業(yè)核心 表結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 規(guī)范存儲(chǔ)的結(jié)構(gòu)化數(shù)據(jù),如數(shù)據(jù)庫(kù)表、Excel 表、 ...
2025-09-17Excel 導(dǎo)入數(shù)據(jù)含缺失值?詳解 dropna 函數(shù)的功能與實(shí)戰(zhàn)應(yīng)用 在用 Python(如 pandas 庫(kù))處理 Excel 數(shù)據(jù)時(shí),“缺失值” 是高頻 ...
2025-09-16深入解析卡方檢驗(yàn)與 t 檢驗(yàn):差異、適用場(chǎng)景與實(shí)踐應(yīng)用 在數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)領(lǐng)域,假設(shè)檢驗(yàn)是驗(yàn)證研究假設(shè)、判斷數(shù)據(jù)差異是否 “ ...
2025-09-16CDA 數(shù)據(jù)分析師:掌控表格結(jié)構(gòu)數(shù)據(jù)全功能周期的專業(yè)操盤手 表格結(jié)構(gòu)數(shù)據(jù)(以 “行 - 列” 存儲(chǔ)的結(jié)構(gòu)化數(shù)據(jù),如 Excel 表、數(shù)據(jù) ...
2025-09-16MySQL 執(zhí)行計(jì)劃中 rows 數(shù)量的準(zhǔn)確性解析:原理、影響因素與優(yōu)化 在 MySQL SQL 調(diào)優(yōu)中,EXPLAIN執(zhí)行計(jì)劃是核心工具,而其中的row ...
2025-09-15解析 Python 中 Response 對(duì)象的 text 與 content:區(qū)別、場(chǎng)景與實(shí)踐指南 在 Python 進(jìn)行 HTTP 網(wǎng)絡(luò)請(qǐng)求開發(fā)時(shí)(如使用requests ...
2025-09-15CDA 數(shù)據(jù)分析師:激活表格結(jié)構(gòu)數(shù)據(jù)價(jià)值的核心操盤手 表格結(jié)構(gòu)數(shù)據(jù)(如 Excel 表格、數(shù)據(jù)庫(kù)表)是企業(yè)最基礎(chǔ)、最核心的數(shù)據(jù)形態(tài) ...
2025-09-15Python HTTP 請(qǐng)求工具對(duì)比:urllib.request 與 requests 的核心差異與選擇指南 在 Python 處理 HTTP 請(qǐng)求(如接口調(diào)用、數(shù)據(jù)爬取 ...
2025-09-12解決 pd.read_csv 讀取長(zhǎng)浮點(diǎn)數(shù)據(jù)的科學(xué)計(jì)數(shù)法問題 為幫助 Python 數(shù)據(jù)從業(yè)者解決pd.read_csv讀取長(zhǎng)浮點(diǎn)數(shù)據(jù)時(shí)的科學(xué)計(jì)數(shù)法問題 ...
2025-09-12CDA 數(shù)據(jù)分析師:業(yè)務(wù)數(shù)據(jù)分析步驟的落地者與價(jià)值優(yōu)化者 業(yè)務(wù)數(shù)據(jù)分析是企業(yè)解決日常運(yùn)營(yíng)問題、提升執(zhí)行效率的核心手段,其價(jià)值 ...
2025-09-12用 SQL 驗(yàn)證業(yè)務(wù)邏輯:從規(guī)則拆解到數(shù)據(jù)把關(guān)的實(shí)戰(zhàn)指南 在業(yè)務(wù)系統(tǒng)落地過程中,“業(yè)務(wù)邏輯” 是連接 “需求設(shè)計(jì)” 與 “用戶體驗(yàn) ...
2025-09-11塔吉特百貨孕婦營(yíng)銷案例:數(shù)據(jù)驅(qū)動(dòng)下的精準(zhǔn)零售革命與啟示 在零售行業(yè) “流量紅利見頂” 的當(dāng)下,精準(zhǔn)營(yíng)銷成為企業(yè)突圍的核心方 ...
2025-09-11CDA 數(shù)據(jù)分析師與戰(zhàn)略 / 業(yè)務(wù)數(shù)據(jù)分析:概念辨析與協(xié)同價(jià)值 在數(shù)據(jù)驅(qū)動(dòng)決策的體系中,“戰(zhàn)略數(shù)據(jù)分析”“業(yè)務(wù)數(shù)據(jù)分析” 是企業(yè) ...
2025-09-11Excel 數(shù)據(jù)聚類分析:從操作實(shí)踐到業(yè)務(wù)價(jià)值挖掘 在數(shù)據(jù)分析場(chǎng)景中,聚類分析作為 “無(wú)監(jiān)督分組” 的核心工具,能從雜亂數(shù)據(jù)中挖 ...
2025-09-10統(tǒng)計(jì)模型的核心目的:從數(shù)據(jù)解讀到?jīng)Q策支撐的價(jià)值導(dǎo)向 統(tǒng)計(jì)模型作為數(shù)據(jù)分析的核心工具,并非簡(jiǎn)單的 “公式堆砌”,而是圍繞特定 ...
2025-09-10CDA 數(shù)據(jù)分析師:商業(yè)數(shù)據(jù)分析實(shí)踐的落地者與價(jià)值創(chuàng)造者 商業(yè)數(shù)據(jù)分析的價(jià)值,最終要在 “實(shí)踐” 中體現(xiàn) —— 脫離業(yè)務(wù)場(chǎng)景的分 ...
2025-09-10機(jī)器學(xué)習(xí)解決實(shí)際問題的核心關(guān)鍵:從業(yè)務(wù)到落地的全流程解析 在人工智能技術(shù)落地的浪潮中,機(jī)器學(xué)習(xí)作為核心工具,已廣泛應(yīng)用于 ...
2025-09-09SPSS 編碼狀態(tài)區(qū)域中 Unicode 的功能與價(jià)值解析 在 SPSS(Statistical Product and Service Solutions,統(tǒng)計(jì)產(chǎn)品與服務(wù)解決方案 ...
2025-09-09