Python數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解
更新時(shí)間:2017年09月12日 10:05:01 作者:方程同調(diào)士
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)結(jié)構(gòu)之單鏈表的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Python數(shù)據(jù)結(jié)構(gòu)之單鏈表的具體代碼,供大家參考,具體內(nèi)容如下
# 節(jié)點(diǎn)類 class Node(): __slots__=['_item','_next'] # 限定Node實(shí)例的屬性 def __init__(self,item): self._item = item self._next = None # Node的指針部分默認(rèn)指向None def getItem(self): return self._item def getNext(self): return self._next def setItem(self,newitem): self._item = newitem def setNext(self,newnext): self._next=newnext # 單鏈表 class SingleLinkedList(): def __init__(self): self._head = None #初始化鏈表為空 始終指向鏈表的頭部 self._size = 0 # 鏈表大小 # 返回鏈表的大小 def size(self): current = self._head count = 0 while current != None: count += 1 current = current.getNext() return count # 遍歷鏈表 def travel(self): current = self._head while current != None: print(current.getItem()) current = current.getNext() # 檢查鏈表是否為空 def isEmpty(self): return self._head == None # 在鏈表前端添加元素 def add(self,item): temp = Node(item) # 創(chuàng)建新的節(jié)點(diǎn) temp.setNext(self._head) # 新創(chuàng)建的next指針指向_head self._head = temp # _head指向新創(chuàng)建的指針 # 在鏈表尾部添加元素 def append(self,item): temp = Node(item) if self.isEmpty(): self._head = temp # 若為空表就直接插入 else: current = self._head while current.getNext() != None: current = current.getNext() # 遍歷列表 current.setNext(temp) # 此時(shí)current為鏈表最后的元素,在末尾插入 # 檢索元素是否在鏈表中 def search(self,item): current = self._head founditem = False while current != None and not founditem: if current.getItem() == item: founditem = True else: current = current.getNext() return founditem # 索引元素在表中的位置 def index(self,item): current = self._head count = 0 found = None while current != None and not found: count += 1 if current.getItem() == item: found = True else: current = current.getNext() if found: return count else: return -1 # 返回-1表示不存在 # 刪除表中的某項(xiàng)元素 def remove(self,item): current = self._head pre = None while current!=None: if current.getItem() == item: if not pre: self._head = current.getNext() else: pre.setNext(current.getNext()) break else: pre = current current = current.getNext() # 在鏈表任意位置插入元素 def insert(self,pos,item): if pos <= 1: self.add(item) elif pos > self.size(): self.append(item) else: temp = Node(item) count = 1 pre = None current = self._head while count < pos: count += 1 pre = current current = current.getNext() pre.setNext(temp) temp.setNext(current) if __name__=='__main__': a=SingleLinkedList() for i in range(1,10): a.append(i) print('鏈表的大小',a.size()) a.travel() print(a.search(6)) print(a.index(5)) a.remove(4) a.travel() a.insert(4,100) a.travel()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python實(shí)現(xiàn)順序表的簡(jiǎn)單代碼
- Python中順序表的實(shí)現(xiàn)簡(jiǎn)單代碼分享
- Python數(shù)據(jù)結(jié)構(gòu)之順序表的實(shí)現(xiàn)代碼示例
- python數(shù)據(jù)結(jié)構(gòu)之線性表的順序存儲(chǔ)結(jié)構(gòu)
- python數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)之實(shí)現(xiàn)線性表的順序
- Python實(shí)現(xiàn)的數(shù)據(jù)結(jié)構(gòu)與算法之鏈表詳解
- python數(shù)據(jù)結(jié)構(gòu)鏈表之單向鏈表(實(shí)例講解)
- Python數(shù)據(jù)結(jié)構(gòu)之翻轉(zhuǎn)鏈表
- python數(shù)據(jù)結(jié)構(gòu)之鏈表詳解
- Python 數(shù)據(jù)結(jié)構(gòu)之旋轉(zhuǎn)鏈表
- Python中順序表原理與實(shí)現(xiàn)方法詳解
相關(guān)文章
Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02python 用遞歸實(shí)現(xiàn)通用爬蟲(chóng)解析器
這篇文章主要介紹了python 用遞歸實(shí)現(xiàn)通用爬蟲(chóng)解析器的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-04-04簡(jiǎn)述Python2與Python3的不同點(diǎn)
在Python2和Python3中都提供print()方法來(lái)打印信息,但兩個(gè)版本間的print稍微有差異。下面通過(guò)本文給大家介紹Python2與Python3的不同點(diǎn),需要的朋友參考下2018-01-01python xlsxwriter創(chuàng)建excel圖表的方法
這篇文章主要為大家詳細(xì)介紹了python xlsxwriter創(chuàng)建excel圖表的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Python使用正則表達(dá)式報(bào)錯(cuò):nothing?to?repeat?at?position?0的解決方案
今天在使用python 正則模塊匹配字符串時(shí)遇到了這個(gè)問(wèn)題,分享給大家,這篇文章主要給大家介紹了關(guān)于Python使用正則表達(dá)式報(bào)錯(cuò)nothing?to?repeat?at?position?0的解決方案,需要的朋友可以參考下2023-03-03