Python實(shí)現(xiàn)的單向循環(huán)鏈表功能示例
本文實(shí)例講述了Python實(shí)現(xiàn)的單向循環(huán)鏈表功能。分享給大家供大家參考,具體如下:
概述:
單向循環(huán)鏈表是指在單鏈表的基礎(chǔ)上,表的最后一個(gè)元素指向鏈表頭結(jié)點(diǎn),不再是為空。
由圖可知,單向循環(huán)鏈表的判斷條件不再是表為空了,而變成了是否到表頭。
操作
is_empty() 判斷鏈表是否為空
length() 返回鏈表的長(zhǎng)度
travel() 遍歷
add(item) 在頭部添加一個(gè)節(jié)點(diǎn)
append(item) 在尾部添加一個(gè)節(jié)點(diǎn)
insert(pos, item) 在指定位置pos添加節(jié)點(diǎn)
remove(item) 刪除一個(gè)節(jié)點(diǎn)
search(item) 查找節(jié)點(diǎn)是否存在
具體代碼:
class Node(object): """節(jié)點(diǎn)""" def __init__(self, item): self.item = item self.next = None class SinCycLinkedlist(object): """單向循環(huán)鏈表""" def __init__(self): self._head = None def is_empty(self): """判斷鏈表是否為空""" return self._head == None def length(self): """返回鏈表的長(zhǎng)度""" # 如果鏈表為空,返回長(zhǎng)度0 if self.is_empty(): return 0 count = 1 cur = self._head while cur.next != self._head: count += 1 cur = cur.next return count def travel(self): """遍歷鏈表""" if self.is_empty(): return cur = self._head print cur.item, while cur.next != self._head: cur = cur.next print cur.item, print "" def add(self, item): """頭部添加節(jié)點(diǎn)""" node = Node(item) if self.is_empty(): self._head = node node.next = self._head else: #添加的節(jié)點(diǎn)指向_head node.next = self._head # 移到鏈表尾部,將尾部節(jié)點(diǎn)的next指向node cur = self._head while cur.next != self._head: cur = cur.next cur.next = node #_head指向添加node的 self._head = node def append(self, item): """尾部添加節(jié)點(diǎn)""" node = Node(item) if self.is_empty(): self._head = node node.next = self._head else: # 移到鏈表尾部 cur = self._head while cur.next != self._head: cur = cur.next # 將尾節(jié)點(diǎn)指向node cur.next = node # 將node指向頭節(jié)點(diǎn)_head node.next = self._head def insert(self, pos, item): """在指定位置添加節(jié)點(diǎn)""" if pos <= 0: self.add(item) elif pos > (self.length()-1): self.append(item) else: node = Node(item) cur = self._head count = 0 # 移動(dòng)到指定位置的前一個(gè)位置 while count < (pos-1): count += 1 cur = cur.next node.next = cur.next cur.next = node def remove(self, item): """刪除一個(gè)節(jié)點(diǎn)""" # 若鏈表為空,則直接返回 if self.is_empty(): return # 將cur指向頭節(jié)點(diǎn) cur = self._head pre = None # 若頭節(jié)點(diǎn)的元素就是要查找的元素item if cur.item == item: # 如果鏈表不止一個(gè)節(jié)點(diǎn) if cur.next != self._head: # 先找到尾節(jié)點(diǎn),將尾節(jié)點(diǎn)的next指向第二個(gè)節(jié)點(diǎn) while cur.next != self._head: cur = cur.next # cur指向了尾節(jié)點(diǎn) cur.next = self._head.next self._head = self._head.next else: # 鏈表只有一個(gè)節(jié)點(diǎn) self._head = None else: pre = self._head # 第一個(gè)節(jié)點(diǎn)不是要?jiǎng)h除的 while cur.next != self._head: # 找到了要?jiǎng)h除的元素 if cur.item == item: # 刪除 pre.next = cur.next return else: pre = cur cur = cur.next # cur 指向尾節(jié)點(diǎn) if cur.item == item: # 尾部刪除 pre.next = cur.next def search(self, item): """查找節(jié)點(diǎn)是否存在""" if self.is_empty(): return False cur = self._head if cur.item == item: return True while cur.next != self._head: cur = cur.next if cur.item == item: return True return False if __name__ == "__main__": ll = SinCycLinkedlist() ll.add(1) ll.add(2) ll.append(3) ll.insert(2, 4) ll.insert(4, 5) ll.insert(0, 6) print "length:",ll.length() ll.travel() print ll.search(3) print ll.search(7) ll.remove(1) print "length:",ll.length() ll.travel()
運(yùn)行結(jié)果:
更多關(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單向循環(huán)鏈表實(shí)例詳解
- Python數(shù)據(jù)結(jié)構(gòu)之循環(huán)鏈表詳解
- python實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)中雙向循環(huán)鏈表操作的示例
- python/golang實(shí)現(xiàn)循環(huán)鏈表的示例代碼
- python單向循環(huán)鏈表原理與實(shí)現(xiàn)方法示例
- Python雙向循環(huán)鏈表實(shí)現(xiàn)方法分析
- Python數(shù)據(jù)結(jié)構(gòu)與算法之鏈表定義與用法實(shí)例詳解【單鏈表、循環(huán)鏈表】
- python雙向鏈表實(shí)例詳解
- Python實(shí)現(xiàn)雙向鏈表基本操作
- python雙向循環(huán)鏈表實(shí)例詳解
相關(guān)文章
解讀FastAPI異步化為transformers模型打造高性能接口
這篇文章主要介紹了解讀FastAPI異步化為transformers模型打造高性能接口問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06python 實(shí)現(xiàn)簡(jiǎn)單的FTP程序
這篇文章主要介紹了python 實(shí)現(xiàn)簡(jiǎn)單的FTP程序,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12簡(jiǎn)單了解Django ORM常用字段類型及參數(shù)配置
這篇文章主要介紹了簡(jiǎn)單了解Django ORM常用字段類型及參數(shù)配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01Python爬蟲抓取代理IP并檢驗(yàn)可用性的實(shí)例
今天小編就為大家分享一篇Python爬蟲抓取代理IP并檢驗(yàn)可用性的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05Django 實(shí)現(xiàn)前端圖片壓縮功能的方法
今天小編就為大家分享一篇Django 實(shí)現(xiàn)前端圖片壓縮功能的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08