免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

怎么在python中實現一個單向循環鏈表-創新互聯

這篇文章將為大家詳細講解有關怎么在python中實現一個單向循環鏈表,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。

創新互聯公司是一家集網站建設,東寧企業網站建設,東寧品牌網站建設,網站定制,東寧網站建設報價,網絡營銷,網絡優化,東寧網站推廣為一體的創新建站企業,幫助傳統企業提升企業形象加強企業競爭力。可充分滿足這一群體相比中小企業更為豐富、高端、多元的互聯網需求。同時我們時刻保持專業、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學習、思考、沉淀、凈化自己,讓我們為更多的企業打造出實用型網站。

單向循環鏈表

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

怎么在python中實現一個單向循環鏈表

操作

  • is_empty() 判斷鏈表是否為空

  • length() 返回鏈表的長度

  • travel() 遍歷

  • add(item) 在頭部添加一個節點

  • append(item) 在尾部添加一個節點

  • insert(pos, item) 在指定位置pos添加節點

  • remove(item) 刪除一個節點

  • search(item) 查找節點是否存在

實現

# -*- coding:utf-8 -*-
#! python3
class Node(object):
  """節點"""
  def __init__(self, item):
    self.item = item
    self.next = None
class 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 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()

運行結果:

length: 6
6
2
1
4
3
5

True
False
length: 5
6
2
4
3
5

關于怎么在python中實現一個單向循環鏈表就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

當前文章:怎么在python中實現一個單向循環鏈表-創新互聯
分享路徑:http://m.newbst.com/article28/cepccp.html

成都網站建設公司_創新互聯,為您提供用戶體驗虛擬主機網站建設網站內鏈企業建站品牌網站制作

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

網站建設網站維護公司