最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python雙鏈表原理與實現(xiàn)方法詳解

 更新時間:2020年02月22日 10:05:09   作者:授我以驢  
這篇文章主要介紹了Python雙鏈表原理與實現(xiàn)方法,結(jié)合實例形式詳細(xì)分析了Python雙鏈表的概念、原理、用法及相關(guān)操作注意事項,需要的朋友可以參考下

本文實例講述了Python雙鏈表原理與實現(xiàn)方法。分享給大家供大家參考,具體如下:

Python實現(xiàn)雙鏈表

文章目錄

單鏈表與雙鏈表比較

  • 雙鏈表比單鏈表多一個前驅(qū)指針位置,空間效率不占優(yōu)勢
  • 由于雙鏈表中的節(jié)點既可以向前也可以向后,相比單鏈表在查找方面效率更高(可使用二分法)

雙鏈表的實現(xiàn)

定義鏈表節(jié)點

  • class Node(object):
      def __init__(self, value=None, prev=None, next=None):
        self.value = value	# 節(jié)點數(shù)據(jù)域
        self.prev = prev	# 節(jié)點前驅(qū)指針
        self.next = next	# 節(jié)點后繼指針
    

初始化雙鏈表

  • 在雙鏈表類的構(gòu)造方法中定義頭指針以及鏈表長度屬性

  • class doubleLinked(object):
    
      def __init__(self):
        self.head = Node()	# 頭指針節(jié)點,用于確定鏈表第一個節(jié)點,不計入鏈表實際長度
        self.length = 0
    

判斷鏈表是否為空

  • 通過實例屬性self.length是否為0判斷鏈表是否為空

  • # 判斷鏈表是否為空
      def is_Empty(self):
        return self.length == 0
    

雙鏈表尾部添加元素

  • 根據(jù)傳入的value值創(chuàng)建node節(jié)點對象

  • 判斷是否為空,若為空則插入節(jié)點的前驅(qū)節(jié)點為head頭指針節(jié)點,head頭指針指向node

  • 如果鏈表非空:

    • 通過while循環(huán)判斷當(dāng)前節(jié)點的后繼節(jié)點是否為None,找到尾節(jié)點
    • 找到尾節(jié)點后,將尾節(jié)點指向待添加節(jié)點,待添加節(jié)點前驅(qū)節(jié)點域指向原偽節(jié)點
    • 長度+1
  • # 尾部添加
      def append(self, value):
        node = Node(value)
        if self.length == 0:
          node.prev = self.head
          self.head.next = node
        else:
          curnode = self.head.next
          while curnode.next != None:
            curnode = curnode.next
          curnode.next = node
          node.prev = curnode
        self.length += 1
    

雙鏈表頭部添加節(jié)點:

  • 調(diào)用類方法is_Empty()判斷是否為空,若為空則直接調(diào)用append()方法

  • 當(dāng)鏈表非空時

    • 首先創(chuàng)建待添加節(jié)點對象node
    • 設(shè)置中間變量存放原頭指針指向的節(jié)點
    • 將頭指針重新指向待添加節(jié)點
    • 新添加節(jié)點后驅(qū)指針域指向中間變量(即原第一個節(jié)點)
    • 中間變量前驅(qū)指針域指向新添加節(jié)點
    • 鏈表長度+1
  • # 頭部添加
      def add(self, value):
        if self.is_Empty():
          self.append(value)
        node = Node(value)
        curnode = self.head.next
        self.head.next = node
        node.next = curnode
        curnode.prev = node
        self.length += 1
    

雙鏈表表頭刪除

  • 老樣子,依舊要先判斷原鏈表是否為空,為空的時候返回False

  • 鏈表不為空時:

    • 將頭指針指向的節(jié)點存放到中間變量curnode
    • 將頭指針指向的節(jié)點指向頭指針(也就是第一個節(jié)點現(xiàn)在變成了頭指針)
    • 新頭指針指向中間變量的后驅(qū)節(jié)點
    • 鏈表長度-1
  • # 刪除表頭節(jié)點
      def remove(self):
        if self.is_Empty():
          return False
        curnode = self.head.next
        self.head = self.head.next
        self.head.next = curnode.next
        self.length -= 1
    

雙鏈表按位置插入

  • 接收兩個位置參數(shù),postion和value

  • 創(chuàng)建待插入節(jié)點對象

  • 變量curnode存放當(dāng)前節(jié)點,變量i初始值為2(位置參數(shù)<2時,默認(rèn)插入到第二個位置,>2時,通過while循環(huán)找到指定位置節(jié)點再進(jìn)行插入)

  • 找到指定位置后,待插入節(jié)點的后驅(qū)指針指向當(dāng)前節(jié)點curnode的后繼節(jié)點,待插入節(jié)點的前驅(qū)節(jié)點為當(dāng)前節(jié)點。

  • 當(dāng)前節(jié)點的原后驅(qū)節(jié)點的前驅(qū)指針域指向待插入節(jié)點,當(dāng)前節(jié)點curnode的后驅(qū)節(jié)點變更為插入節(jié)點

  • 鏈表長度+1

  • # 插入到指定位置
      def insert(self, postion, value):
        node = Node(value)
        curnode = self.head.next
        i = 2
        while i < postion:
          i += 1
          curnode = curnode.next
        node.next = curnode.next
        node.prev = curnode
        curnode.next.prev = node
        curnode.next = node
        self.length += 1
    

雙鏈表刪除指定節(jié)點

  • 依舊需要判斷是否為空,為空時返回False

  • 鏈表不為空時:

    • 設(shè)置中間變量curnode存放當(dāng)前節(jié)點
    • while循環(huán)找到相匹配的值的節(jié)點
    • 找到相應(yīng)節(jié)點后,應(yīng)刪除節(jié)點的前驅(qū)節(jié)點的后繼節(jié)點更改為應(yīng)刪除節(jié)點的后繼節(jié)點;應(yīng)刪除節(jié)點的后繼節(jié)點的前驅(qū)更改為應(yīng)刪除節(jié)點的前驅(qū);
    • 應(yīng)刪除節(jié)點后繼指針指向自己
    • 鏈表長度-1
  • # 刪除指定節(jié)點
      def delete(self, value):
        if self.is_Empty():
          return False
        curnode = self.head.next
        while curnode.value != value:
          curnode = curnode.next
        curnode.prev.next = curnode.next
        curnode.next.prev = curnode.prev
        curnode.next = curnode
        self.length -= 1
    

完整代碼

class Node(object):
  def __init__(self, value=None, prev=None, next=None):
    self.value = value
    self.prev = prev
    self.next = next


class doubleLinked(object):

  def __init__(self):
    self.head = Node()
    self.length = 0

  def __iter__(self):
    for node in self.iter_node():
      yield node.value

  # 對鏈表進(jìn)行可迭代操作
  def iter_node(self):
    curnode = self.head.next
    while curnode.next != None:
      yield curnode
      curnode = curnode.next
    if curnode.next == None:
      yield curnode

  # 判斷鏈表是否為空
  def is_Empty(self):
    return self.length == 0

  # 尾部添加
  def append(self, value):
    node = Node(value)
    if self.length == 0:
      node.prev = self.head
      self.head.next = node
    else:
      curnode = self.head.next
      while curnode.next != None:
        curnode = curnode.next
      curnode.next = node
      node.prev = curnode
    self.length += 1

  # 頭部添加
  def add(self, value):
    if self.is_Empty():
      self.append(value)
    node = Node(value)
    curnode = self.head.next
    self.head.next = node
    node.next = curnode
    curnode.prev = node
    self.length += 1

  # 插入到指定位置
  def insert(self, postion, value):
    node = Node(value)
    curnode = self.head.next
    i = 2
    while i < postion:
      i += 1
      curnode = curnode.next
    node.next = curnode.next
    node.prev = curnode
    curnode.next.prev = node
    curnode.next = node
    self.length += 1
  # 刪除表頭節(jié)點
  def remove(self):
    if self.is_Empty():
      return False
    curnode = self.head.next
    self.head = self.head.next
    self.head.next = curnode.next
    self.length -= 1

  # 刪除指定節(jié)點
  def delete(self, value):
    if self.is_Empty():
      return False
    curnode = self.head.next
    while curnode.value != value:
      curnode = curnode.next
    curnode.prev.next = curnode.next
    curnode.next.prev = curnode.prev
    curnode.next = curnode
    self.length -= 1

# 測試
linkedlist = doubleLinked()
print(linkedlist.is_Empty())
linkedlist.append(1)
linkedlist.append(3)
linkedlist.append(5)
linkedlist.add(4)
linkedlist.add(2)
linkedlist.insert(3,10)
linkedlist.remove()
linkedlist.delete(3)
# 遍歷打印
i = 1
for node in linkedlist:
  print("第%d個鏈表節(jié)點的值: %d"%(i, node))
  i += 1

運(yùn)行結(jié)果:

True
第1個鏈表節(jié)點的值: 4
第2個鏈表節(jié)點的值: 10
第3個鏈表節(jié)點的值: 1
第4個鏈表節(jié)點的值: 5

更多關(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)典教程

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • python中dict字典的查詢鍵值對 遍歷 排序 創(chuàng)建 訪問 更新 刪除基礎(chǔ)操作方法

    python中dict字典的查詢鍵值對 遍歷 排序 創(chuàng)建 訪問 更新 刪除基礎(chǔ)操作方法

    字典的每個鍵值(key=>value)對用冒號(:)分割,每個對之間用逗號(,)分割,整個字典包括在花括號({})中,本文講述了python中dict字典的查詢鍵值對 遍歷 排序 創(chuàng)建 訪問 更新 刪除基礎(chǔ)操作方法
    2018-09-09
  • Python中的默認(rèn)參數(shù)實例分析

    Python中的默認(rèn)參數(shù)實例分析

    這篇文章主要介紹了Python中的默認(rèn)參數(shù)實例分析,分享了相關(guān)代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • python中如何調(diào)用ansys

    python中如何調(diào)用ansys

    這篇文章主要介紹了python中如何調(diào)用ansys問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 使用selenium模擬動態(tài)登錄百度頁面的實現(xiàn)

    使用selenium模擬動態(tài)登錄百度頁面的實現(xiàn)

    本文主要介紹了使用selenium模擬動態(tài)登錄百度頁面,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • python采集微信公眾號文章

    python采集微信公眾號文章

    這篇文章主要為大家詳細(xì)介紹了python采集微信公眾號文章的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Python 文件批量處理操作的實現(xiàn)示例

    Python 文件批量處理操作的實現(xiàn)示例

    Python提供了豐富的工具來處理文件批量操作,包括批量重命名文件、移動文件和修改文件內(nèi)容,具有一定的參考價值,感興趣的可以了解一下
    2024-12-12
  • 用python打印菱形的實操方法和代碼

    用python打印菱形的實操方法和代碼

    在本篇文章里小編給大家分享了關(guān)于用python打印菱形的實操方法和代碼,對此有需要的朋友們可以學(xué)習(xí)下。
    2019-06-06
  • 在DOS界面如何運(yùn)行python的py文件

    在DOS界面如何運(yùn)行python的py文件

    這篇文章主要介紹了在DOS界面如何運(yùn)行python的py文件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Python如何實現(xiàn)定時器功能

    Python如何實現(xiàn)定時器功能

    在本篇文章里小編給大家分享的是關(guān)于Python中的簡單定時器實例及代碼,需要的朋友們可以學(xué)習(xí)下。
    2020-05-05
  • Python實現(xiàn)字典的key和values的交換

    Python實現(xiàn)字典的key和values的交換

    本文給大家分別介紹了在python3.0和2.7版本下實現(xiàn)字典的key和values的交換的程序代碼,非常的簡單實用,有需要的小伙伴可以參考下。
    2015-08-08

最新評論

衡山县| 云霄县| 武功县| 集安市| 清新县| 翁牛特旗| 会泽县| 和田县| 康平县| 辉南县| 依兰县| 神池县| 奉贤区| 偃师市| 枞阳县| 东港市| 西盟| 吉木乃县| 昌黎县| 皮山县| 临邑县| 平原县| 伊吾县| 长岛县| 河间市| 阿巴嘎旗| 静宁县| 大冶市| 开鲁县| 霍林郭勒市| 肥城市| 武宁县| 宜川县| 仙游县| 沧州市| 全椒县| 三台县| 深水埗区| 湖州市| 丁青县| 澎湖县|