国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Python > 正文

python單向循環鏈表原理與實現方法示例

2024-09-09 19:02:54
字體:
來源:轉載
供稿:網友

本文實例講述了python單向循環鏈表原理與實現方法。分享給大家供大家參考,具體如下:

單向循環鏈表

單鏈表的一個變形是單向循環鏈表,鏈表中最后一個節點的next域不再為None,而是指向鏈表的頭節點。

操作

is_empty() 判斷鏈表是否為空 length() 返回鏈表的長度 travel() 遍歷 add(item) 在頭部添加一個節點 append(item) 在尾部添加一個節點 insert(pos, item) 在指定位置pos添加節點 remove(item) 刪除一個節點 search(item) 查找節點是否存在

實現

# -*- coding:utf-8 -*-#! python3class Node(object):  """節點"""  def __init__(self, item):    self.item = item    self.next = Noneclass SinCycLinkedlist(object):  """單向循環鏈表"""  def __init__(self):    self.__head = None  def is_empty(self):    """判斷鏈表是否為空"""    return self.__head == None  def length(self):    """返回鏈表的長度"""    # 如果鏈表為空,返回長度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):    """頭部添加節點"""    node = Node(item)    if self.is_empty():      self.__head = node      node.next = self.__head    else:      # 添加的節點指向_head      node.next = self.__head      # 移到鏈表尾部,將尾部節點的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):    """尾部添加節點"""    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      # 將尾節點指向node      cur.next = node      # 將node指向頭節點_head      node.next = self.__head  def insert(self, pos, item):    """在指定位置添加節點"""    if pos <= 0:      self.add(item)    elif pos > (self.length() - 1):      self.append(item)    else:      node = Node(item)      cur = self.__head      count = 0      # 移動到指定位置的前一個位置      while count < (pos - 1):        count += 1        cur = cur.next      node.next = cur.next      cur.next = node  def remove(self, item):    """刪除一個節點"""    # 若鏈表為空,則直接返回    if self.is_empty():      return    # 將cur指向頭節點    cur = self.__head    pre = None    while cur.next != self.__head:      if cur.item == item:        # 先判斷此結點是否是頭節點        if cur == self.__head:          # 頭節點的情況          # 找尾節點          rear = self.__head          while rear.next != self.__head:            rear = rear.next          self.__head = cur.next          rear.next = self.__head        else:          # 中間節點          pre.next = cur.next        return      else:        pre = cur        cur = cur.next    # 退出循環,cur指向尾節點    if cur.item == item:      if cur == self.__head:        # 鏈表只有一個節點        self.__head = None      else:        # pre.next = cur.next        pre.next = self.__head  def search(self, item):    """查找節點是否存在"""    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 Falseif __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()
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 英吉沙县| 洪湖市| 万宁市| 封开县| 米脂县| 墨江| 惠来县| 肥城市| 四子王旗| 巴彦县| 南川市| 镶黄旗| 新化县| 兴文县| 吴川市| 于都县| 武安市| 大同市| 阿克陶县| 雅安市| 灌南县| 内丘县| 称多县| 金寨县| 波密县| 绩溪县| 扶绥县| 那曲县| 鄂尔多斯市| 彭泽县| 漯河市| 广丰县| 锦州市| 西乡县| 通许县| 藁城市| 常熟市| 双流县| 当涂县| 徐闻县| 石首市|