Python單鏈表簡單實現(xiàn)代碼
本文實例講述了Python單鏈表簡單實現(xiàn)代碼。分享給大家供大家參考,具體如下:
用Python模擬一下單鏈表,比較簡單,初學(xué)者可以參考參考
#coding:utf-8
class Node(object):
def __init__(self, data):
self.data = data
self.next = None
class NodeList(object):
def __init__(self, node):
self.head = node
self.head.next = None
self.end = self.head
def add_node(self, node):
self.end.next = node
self.end = self.end.next
def length(self):
node = self.head
count = 1
while node.next is not None:
count += 1
node = node.next
return count
# delete node and return it's value
def delete_node(self, index):
if index+1 > self.length():
raise IndexError('index out of bounds')
i = 0
node = self.head
while True:
if i==index-1:
break
node = node.next
i += 1
tmp_node = node.next
node.next = node.next.next
return tmp_node.data
def show(self):
node = self.head
node_str = ''
while node is not None:
if node.next is not None:
node_str += str(node.data) + '->'
else:
node_str += str(node.data)
node = node.next
print node_str
# Modify the original position value and return the old value
def change(self, index, data):
if index+1 > self.length():
raise IndexError('index out of bounds')
i = 0
node = self.head
while True:
if i == index:
break
node = node.next
i += 1
tmp_data = node.data
node.data = data
return tmp_data
# To find the location of index value
def find(self, index):
if index+1 > self.length():
raise IndexError('index out of bounds')
i = 0
node = self.head
while True:
if i == index:
break
node = node.next
i += 1
return node.data
#test case
n1 = Node(0)
n2 = Node(1)
n3 = Node(2)
n4 = Node(3)
n5 = Node(4)
node_list = NodeList(n1)
node_list.add_node(n2)
node_list.add_node(n3)
node_list.add_node(n4)
node_list.add_node(n5)
#node = node_list.delete_node(3)
#print node
#d = node_list.change(0,88)
data = node_list.find(5)
print data
node_list.show()
更多關(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文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
- 淺談Python單向鏈表的實現(xiàn)
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實例講解)
- python實現(xiàn)單向鏈表詳解
- Python單向鏈表和雙向鏈表原理與用法實例詳解
- python實現(xiàn)反轉(zhuǎn)部分單向鏈表
- python判斷單向鏈表是否包括環(huán),若包含則計算環(huán)入口的節(jié)點實例分析
- python實現(xiàn)獲取單向鏈表倒數(shù)第k個結(jié)點的值示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實現(xiàn)
- Python實現(xiàn)針對給定單鏈表刪除指定節(jié)點的方法
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實例詳解【單鏈表、循環(huán)鏈表】
- python實現(xiàn)單鏈表中刪除倒數(shù)第K個節(jié)點的方法
- python單向鏈表的基本實現(xiàn)與使用方法【定義、遍歷、添加、刪除、查找等】
相關(guān)文章
使用python加密主機(jī)文件幾種方法實現(xiàn)
本文主要介紹了使用python加密主機(jī)文件幾種方法實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
Python調(diào)用olmOCR大模型實現(xiàn)提取復(fù)雜PDF文件內(nèi)容
olmocr是由Allen人工智能研究所(AI2)開發(fā)的一個開源工具包,旨在高效地將PDF和其他文檔轉(zhuǎn)換為結(jié)構(gòu)化的純文本,同時保持自然閱讀順序,下面我們來看看如何使用olmOCR大模型實現(xiàn)提取復(fù)雜PDF文件內(nèi)容吧2025-03-03
解決Python運行文件出現(xiàn)out of memory框的問題
今天小編就為大家分享一篇解決Python運行文件出現(xiàn)out of memory框的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python位置參數(shù)與關(guān)鍵字參數(shù)的區(qū)別
文主要介紹了Python函數(shù)參數(shù)的兩種基本類型:位置參數(shù)和關(guān)鍵字參數(shù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01

