看此博客之前建议先看看B站的视频python数据结构与算法系列课程,该课程中未实现双向循环链表的操作,所以我按照该视频的链表思路实现了双向循环链表的操作,欢迎大家阅读与交流,如有侵权,请联系博主!
下面附上代码:
class Node: def __init__(self, elem): self.elem = elem self.prev = None self.next = None class DoubleCycleLinkList: def __init__(self, node=None): self.__head = node def is_empty(self): """判空""" if self.__head is None: return True return False def length(self): """链表长度""" if self.is_empty(): return 0 cur = self.__head count = 1 while cur.next is not self.__head: count += 1 cur = cur.next return count def travel(self): """遍历链表""" if self.is_empty(): return cur = self.__head while cur.next is not self.__head: print(cur.elem, end=" ") cur = cur.next print(cur.elem, end=" ") print("") def add(self, elem): """头插法""" node = Node(elem) if self.is_empty(): self.__head = node node.prev = node node.next = node else: self.__head.prev.next = node node.prev = self.__head.prev node.next = self.__head self.__head.prev = node self.__head = node def append(self, elem): """尾插法""" node = Node(elem) if self.is_empty(): self.__head = node node.prev = node node.next = node else: node.next = self.__head node.prev = self.__head.prev self.__head.prev.next = node self.__head.prev = node def insert(self, pos, elem): """任一位置(pos)插入, 下标从0数起""" if pos <= 0: self.add(elem) elif pos > (self.length() - 1): self.append(elem) else: count = 0 cur = self.__head node = Node(elem) while count < (pos - 1): count += 1 cur = cur.next node.next = cur.next node.prev = cur node.next.prev = node cur.next = node def remove(self, elem): """删除某一节点,若有多个符合条件的节点,删除第一个即可""" if self.is_empty(): return cur = self.__head while cur.next is not self.__head: if cur.elem == elem: if cur is self.__head: self.__head = cur.next cur.prev.next = cur.next cur.next.prev = cur.prev else: cur.prev.next = cur.next cur.next.prev = cur.prev break cur = cur.next if cur.elem == elem: cur.prev.next = self.__head self.head = cur.prev def search(self, elem): """查找某一个节点""" if self.is_empty(): return False cur = self.__head while cur.next is not self.__head: if cur.elem == elem: return True cur = cur.next # while中处理不到尾节点,所以进行最后尾节点的判断 if cur.elem == elem: return True return False
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月18日
2024年11月18日
- 群星《天域原音·聆听雪域藏歌STS+SRS》[WAV+CUE]
- 尤长靖.2020-AZORAland·我是尤长靖【香蕉娱乐】【FLAC分轨】
- 群星.1995-台北爱情故事【飞碟】【WAV+CUE】
- 群星.2024-锦绣安宁电视剧原声带【D-Jin.Music】【FLAC分轨】
- 群星《闪光的夏天 第4期》[320K/MP3][97.82MB]
- 群星《闪光的夏天 第4期》[FLAC/分轨][553.31MB]
- 群星《奔赴!万人现场 第4期》[320K/MP3][80.75MB]
- 林琳《独角戏HQ》WAV
- FIM-《Super-Sound-3》声霸3[WAV+CUE]
- 喇叭花-绝版天碟《我的碟“MyDisc”》[正版原抓WAV+CUE]
- 陈慧琳.1999-真感觉【正东】【WAV+CUE】
- 徐玮.1986-走自己的路(喜玛拉雅复刻版)【同心圆】【WAV+CUE】
- 林海峰.2003-我撑你【EMI百代】【WAV+CUE】
- 群星《奔赴!万人现场 第4期》[FLAC/分轨][454.89MB]
- 腾讯音乐人《未来立体声·Stereo Future VOL.12》[320K/MP3][62.37MB]