欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

Python實(shí)現(xiàn)雙鏈表

文章目錄

單鏈表與雙鏈表比較

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

雙鏈表的實(shí)現(xiàn)

定義鏈表節(jié)點(diǎn)

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

初始化雙鏈表

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

  • class doubleLinked(object):
    
      def __init__(self):
        self.head = Node()	# 頭指針節(jié)點(diǎn),用于確定鏈表第一個(gè)節(jié)點(diǎn),不計(jì)入鏈表實(shí)際長(zhǎng)度
        self.length = 0
    

判斷鏈表是否為空

  • 通過(guò)實(shí)例屬性self.length是否為0判斷鏈表是否為空

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

雙鏈表尾部添加元素

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

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

  • 如果鏈表非空:

    • 通過(guò)while循環(huán)判斷當(dāng)前節(jié)點(diǎn)的后繼節(jié)點(diǎn)是否為None,找到尾節(jié)點(diǎn)
    • 找到尾節(jié)點(diǎn)后,將尾節(jié)點(diǎn)指向待添加節(jié)點(diǎn),待添加節(jié)點(diǎn)前驅(qū)節(jié)點(diǎn)域指向原偽節(jié)點(diǎn)
    • 長(zhǎng)度+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ǎn):

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

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

    • 首先創(chuàng)建待添加節(jié)點(diǎn)對(duì)象node
    • 設(shè)置中間變量存放原頭指針指向的節(jié)點(diǎn)
    • 將頭指針重新指向待添加節(jié)點(diǎn)
    • 新添加節(jié)點(diǎn)后驅(qū)指針域指向中間變量(即原第一個(gè)節(jié)點(diǎn))
    • 中間變量前驅(qū)指針域指向新添加節(jié)點(diǎn)
    • 鏈表長(zhǎng)度+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
    

雙鏈表表頭刪除

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

  • 鏈表不為空時(shí):

    • 將頭指針指向的節(jié)點(diǎn)存放到中間變量curnode
    • 將頭指針指向的節(jié)點(diǎn)指向頭指針(也就是第一個(gè)節(jié)點(diǎn)現(xiàn)在變成了頭指針)
    • 新頭指針指向中間變量的后驅(qū)節(jié)點(diǎn)
    • 鏈表長(zhǎng)度-1
  • # 刪除表頭節(jié)點(diǎn)
      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
    

雙鏈表按位置插入

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

  • 創(chuàng)建待插入節(jié)點(diǎn)對(duì)象

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

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

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

  • 鏈表長(zhǎng)度+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é)點(diǎn)

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

  • 鏈表不為空時(shí):

    • 設(shè)置中間變量curnode存放當(dāng)前節(jié)點(diǎn)
    • while循環(huán)找到相匹配的值的節(jié)點(diǎn)
    • 找到相應(yīng)節(jié)點(diǎn)后,應(yīng)刪除節(jié)點(diǎn)的前驅(qū)節(jié)點(diǎn)的后繼節(jié)點(diǎn)更改為應(yīng)刪除節(jié)點(diǎn)的后繼節(jié)點(diǎn);應(yīng)刪除節(jié)點(diǎn)的后繼節(jié)點(diǎn)的前驅(qū)更改為應(yīng)刪除節(jié)點(diǎn)的前驅(qū);
    • 應(yīng)刪除節(jié)點(diǎn)后繼指針指向自己
    • 鏈表長(zhǎng)度-1
  • # 刪除指定節(jié)點(diǎn)
      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

  # 對(duì)鏈表進(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é)點(diǎn)
  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é)點(diǎn)
  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

# 測(cè)試
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個(gè)鏈表節(jié)點(diǎn)的值: %d"%(i, node))
  i += 1

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

True
第1個(gè)鏈表節(jié)點(diǎn)的值: 4
第2個(gè)鏈表節(jié)點(diǎn)的值: 10
第3個(gè)鏈表節(jié)點(diǎn)的值: 1
第4個(gè)鏈表節(jié)點(diǎn)的值: 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)典教程

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

相關(guān)文章

  • 完美解決pycharm導(dǎo)入自己寫的py文件爆紅問題

    完美解決pycharm導(dǎo)入自己寫的py文件爆紅問題

    今天小編就為大家分享一篇完美解決pycharm導(dǎo)入自己寫的py文件爆紅問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • Python基于二分查找實(shí)現(xiàn)求整數(shù)平方根的方法

    Python基于二分查找實(shí)現(xiàn)求整數(shù)平方根的方法

    這篇文章主要介紹了Python基于二分查找實(shí)現(xiàn)求整數(shù)平方根的方法,涉及Python的二分查找算法與數(shù)學(xué)運(yùn)算相關(guān)技巧,需要的朋友可以參考下
    2016-05-05
  • python多進(jìn)程實(shí)現(xiàn)文件下載傳輸功能

    python多進(jìn)程實(shí)現(xiàn)文件下載傳輸功能

    這篇文章主要為大家詳細(xì)介紹了python多進(jìn)程實(shí)現(xiàn)文件下載傳輸功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • pyqt5 QlistView列表顯示的實(shí)現(xiàn)示例

    pyqt5 QlistView列表顯示的實(shí)現(xiàn)示例

    這篇文章主要介紹了pyqt5 QlistView列表顯示的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • 教你怎么用Python生成九宮格照片

    教你怎么用Python生成九宮格照片

    過(guò)年過(guò)節(jié)大家的朋友圈是不是特別熱鬧,每當(dāng)小編看見朋友圈有這種九宮格的照片就覺得特別秀,一直想自己什么時(shí)候也能來(lái)秀一個(gè),所以直接拿這個(gè)練練手,酷炸朋友圈一波,直接進(jìn)入主題,需要的朋友可以參考下
    2021-05-05
  • Python編寫單元測(cè)試代碼實(shí)例

    Python編寫單元測(cè)試代碼實(shí)例

    這篇文章主要介紹了Python編寫單元測(cè)試代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • python操作excel之xlwt與xlrd

    python操作excel之xlwt與xlrd

    這篇文章主要介紹了python使用xlwt與xlrd操作excel,需要的朋友可以參考下
    2022-12-12
  • python 將html轉(zhuǎn)換為pdf的幾種方法

    python 將html轉(zhuǎn)換為pdf的幾種方法

    這篇文章主要介紹了python 將html轉(zhuǎn)換為pdf的幾種方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-12-12
  • python計(jì)算質(zhì)數(shù)的6種方法

    python計(jì)算質(zhì)數(shù)的6種方法

    本文主要介紹了python計(jì)算質(zhì)數(shù)的6種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • Python算法之圖的遍歷

    Python算法之圖的遍歷

    這篇文章主要介紹了Python算法之圖的遍歷,涉及遍歷算法BFS和DFS,以及尋找圖的(強(qiáng))連通分量的算法等相關(guān)內(nèi)容,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-11-11

最新評(píng)論