python單向鏈表的基本實(shí)現(xiàn)與使用方法【定義、遍歷、添加、刪除、查找等】
本文實(shí)例講述了python單向鏈表的基本實(shí)現(xiàn)與使用方法。分享給大家供大家參考,具體如下:
# -*- coding:utf-8 -*- #! python3 class Node(): def __init__(self,item): #初始化這個(gè)節(jié)點(diǎn),值和下一個(gè)指向 self.item = item self.next = None class SingleLinklist(): def __init__(self): #初始化這個(gè)單鏈表的頭指針為空 self._head = None def length(self): #獲取這個(gè)鏈表的長(zhǎng)度 count = 0 cur = self._head while cur != None: count+=1 cur = cur.next return count def is_empty(self): """判斷是否為空""" return self._head == None def add(self,item): """在頭部添加元素""" node = Node(item) node.next = self._head self._head = node def append(self,item): """在尾部添加元素""" cur = self._head node = Node(item) while cur != None: cur = cur.next cur.next = node def insert(self,pos,item): """在選定的位置添加元素""" cur = self._head node = Node(item) count = 0 if pos <= 0: self.add(item) elif pos > (self.length()-1): self.append(item) else: while count < (pos -1): count+=1 cur = cur.next node.next = cur.next cur.next = node def travel(self): """遍歷整個(gè)鏈表""" cur = self._head while cur != None: print(cur.item,end=" ") cur = cur.next print(" ") def remove(self,item): """刪除鏈表""" cur = self._head pre =None while cur != None: if cur.item == item: if not pre: self._head = cur.next break else: pre.next = cur.next else: pre = cur # cur = cur.next def search(self,item): """查找某個(gè)節(jié)點(diǎn)""" cur = self._head while cur != None: if cur.item == item: print("找到這個(gè)元素了") return True cur = cur.next print("抱歉沒有這個(gè)元素") return False singlistdemo = SingleLinklist() singlistdemo.add(1) singlistdemo.add(2) singlistdemo.add(65) singlistdemo.insert(2,77) singlistdemo.insert(1,66) singlistdemo.insert(0,66) print(singlistdemo.length()) singlistdemo.travel() singlistdemo.remove(1) singlistdemo.travel() singlistdemo.search(65)
運(yùn)行結(jié)果:
6
66 65 66 2 77 1
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
- python中的單向鏈表實(shí)現(xiàn)
- python如何實(shí)現(xiàn)單向鏈表及單向鏈表的反轉(zhuǎn)
- python實(shí)現(xiàn)獲取單向鏈表倒數(shù)第k個(gè)結(jié)點(diǎn)的值示例
- python判斷單向鏈表是否包括環(huán),若包含則計(jì)算環(huán)入口的節(jié)點(diǎn)實(shí)例分析
- python實(shí)現(xiàn)反轉(zhuǎn)部分單向鏈表
- Python單向鏈表和雙向鏈表原理與用法實(shí)例詳解
- python實(shí)現(xiàn)單向鏈表詳解
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)
- 淺談Python單向鏈表的實(shí)現(xiàn)
- Python實(shí)現(xiàn)單向鏈表
相關(guān)文章
Python的Flask框架中SQLAlchemy使用時(shí)的亂碼問題解決
這篇文章主要介紹了Python的Flask框架中SQLAlchemy使用時(shí)的亂碼問題解決,SQLAlchemy與Python結(jié)合對(duì)數(shù)據(jù)庫的操作非常方便,需要的朋友可以參考下2015-11-11通過實(shí)例學(xué)習(xí)Python Excel操作
這篇文章主要介紹了通過實(shí)例學(xué)習(xí)Python Excel操作,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01使用Python圖像處理庫Pillow處理圖像文件的案例分析
本文將通過使用Python圖像處理庫Pillow,幫助大家進(jìn)一步了解Python的基本概念:模塊、對(duì)象、方法和函數(shù)的使用,文中代碼講解的非常詳細(xì),需要的朋友可以參考下2023-07-07pyqt5對(duì)用qt designer設(shè)計(jì)的窗體實(shí)現(xiàn)彈出子窗口的示例
今天小編就為大家分享一篇pyqt5對(duì)用qt designer設(shè)計(jì)的窗體實(shí)現(xiàn)彈出子窗口的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python tkinter實(shí)現(xiàn)界面切換的示例代碼
今天小編就為大家分享一篇python tkinter實(shí)現(xiàn)界面切換的示例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06Python使用ffmpeg合成視頻、音頻的實(shí)現(xiàn)方法
這篇文章主要介紹了Python使用ffmpeg合成視頻、音頻,通過本文的學(xué)習(xí)能幫助大家了解如何在python中調(diào)用ffmpeg模塊,對(duì)此進(jìn)行音視頻合并,完成視頻合成,需要的朋友可以參考下2022-04-04