Python單鏈表簡單實(shí)現(xiàn)代碼
本文實(shí)例講述了Python單鏈表簡單實(shí)現(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è)計(jì)有所幫助。
- 淺談Python單向鏈表的實(shí)現(xiàn)
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)
- python實(shí)現(xiàn)單向鏈表詳解
- Python單向鏈表和雙向鏈表原理與用法實(shí)例詳解
- python實(shí)現(xiàn)反轉(zhuǎn)部分單向鏈表
- python判斷單向鏈表是否包括環(huán),若包含則計(jì)算環(huán)入口的節(jié)點(diǎn)實(shí)例分析
- python實(shí)現(xiàn)獲取單向鏈表倒數(shù)第k個(gè)結(jié)點(diǎn)的值示例
- Python數(shù)據(jù)結(jié)構(gòu)與算法之列表(鏈表,linked list)簡單實(shí)現(xiàn)
- Python實(shí)現(xiàn)針對給定單鏈表刪除指定節(jié)點(diǎn)的方法
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實(shí)例詳解【單鏈表、循環(huán)鏈表】
- python實(shí)現(xiàn)單鏈表中刪除倒數(shù)第K個(gè)節(jié)點(diǎn)的方法
- python單向鏈表的基本實(shí)現(xiàn)與使用方法【定義、遍歷、添加、刪除、查找等】
相關(guān)文章
jupyter 中文亂碼設(shè)置編碼格式 避免控制臺輸出的解決
這篇文章主要介紹了jupyter 中文亂碼設(shè)置編碼格式 避免控制臺輸出的解決,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python使用MySQL8.2讀寫分離實(shí)現(xiàn)示例詳解
在這篇文章中,我們將了解如何將?MySQL?8.2?的讀寫分離功能與?MySQL-Connector/Python?一起使用的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11python使用opencv換照片底色的實(shí)現(xiàn)
這篇文章主要介紹了python使用opencv換照片底色的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11python爬蟲爬取淘寶商品信息(selenum+phontomjs)
這篇文章主要為大家詳細(xì)介紹了python爬蟲爬取淘寶商品信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02Python 使用 docopt 解析json參數(shù)文件過程講解
這篇文章主要介紹了Python 使用 docopt 解析json參數(shù)文件過程講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08python批量提取圖片信息并保存的實(shí)現(xiàn)
這篇文章主要介紹了python批量提取圖片信息并保存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法
這篇文章主要介紹了Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09