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

python如何對鏈表操作

 更新時間:2020年10月10日 09:12:29   作者:南方的墻  
這篇文章主要介紹了python如何對鏈表操作,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

鏈表

鏈表(linked list)是由一組被稱為結點的數據元素組成的數據結構,每個結點都包含結點本身的信息和指向下一個結點的地址。
由于每個結點都包含了可以鏈接起來的地址信息,所以用一個變量就能夠訪問整個結點序列。
也就是說,結點包含兩部分信息:一部分用于存儲數據元素的值,稱為信息域;另一部分用于存儲下一個數據元素地址的指針,稱為指針域。鏈表中的第一個結點的地址存儲在一個單獨的結點中,稱為頭結點或首結點。鏈表中的最后一個結點沒有后繼元素,其指針域為空。

代碼

class Node():
  '創(chuàng)建節(jié)點'

  def __init__(self, data):
    self.data = data
    self.next = None


class LinkList():
  '創(chuàng)建列表'

  def __init__(self, node):
    '初始化列表'
    self.head = node    #鏈表的頭部
    self.head.next = None
    self.tail = self.head #記錄鏈表的尾部

  def add_node(self, node):
    '添加節(jié)點'
    self.tail.next = node
    self.tail = self.tail.next

  def view(self):
    '查看列表'
    node = self.head
    link_str = ''
    while node is not None:
      if node.next is not None:
        link_str += str(node.data) + '-->'
      else:
        link_str += str(node.data)
      node = node.next
    print('The Linklist is:' + link_str)

  def length(self):
    '列表長度'
    node = self.head
    count = 1
    while node.next is not None:
      count += 1
      node = node.next
    print('The length of linklist are %d' % count)
    return count

  def delete_node(self, index):
    '刪除節(jié)點'
    if index + 1 > self.length():
      raise IndexError('index out of bounds')
    num = 0
    node = self.head
    while True:
      if num == index - 1:
        break
      node = node.next
      num += 1
    tmp_node = node.next
    node.next = node.next.next
    return tmp_node.data

  def find_node(self, index):
    '查看具體節(jié)點'
    if index + 1 > self.length():
      raise IndexError('index out of bounds')
    num = 0
    node = self.head
    while True:
      if num == index:
        break
      node = node.next
      num += 1
    return node.data


node1 = Node(3301)
node2 = Node(330104)
node3 = Node(330104005)
node4 = Node(330104005052)
node5 = Node(330104005052001)

linklist = LinkList(node1)
linklist.add_node(node2)
linklist.add_node(node3)
linklist.add_node(node4)
linklist.add_node(node5)


linklist.view()
linklist.length()

以上就是python如何對鏈表操作的詳細內容,更多關于python 鏈表操作的資料請關注腳本之家其它相關文章!

相關文章

  • 淺談Python中的bs4基礎

    淺談Python中的bs4基礎

    今天小編就為大家分享一篇關于Python中的bs4基礎,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-10-10
  • Python使用openpyxl模塊處理Excel文件

    Python使用openpyxl模塊處理Excel文件

    這篇文章介紹了Python使用openpyxl模塊處理Excel文件的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 關于python中的xpath解析定位

    關于python中的xpath解析定位

    這篇文章主要介紹了關于python中的xpath解析定位,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • python刪除列表中特定元素的幾種方法

    python刪除列表中特定元素的幾種方法

    這篇文章主要介紹了python刪除列表中特定元素的幾種方法,文章圍繞主題展開詳細的內容介紹,具有一定的參考價價值,需要的小伙伴可以參考一下
    2022-05-05
  • python模塊之time模塊(實例講解)

    python模塊之time模塊(實例講解)

    下面小編就為大家?guī)硪黄猵ython模塊之time模塊(實例講解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • Python設計模式編程中解釋器模式的簡單程序示例分享

    Python設計模式編程中解釋器模式的簡單程序示例分享

    這篇文章主要介紹了Python設計模式編程中解釋器模式的簡單程序示例分享,解釋器模式強調用抽象類來表達程序中將要實現的功能,需要的朋友可以參考下
    2016-03-03
  • python基于win32實現窗口截圖

    python基于win32實現窗口截圖

    這篇文章主要為大家詳細介紹了python基于win32api實現窗口截圖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Python并發(fā)之多進程的方法實例代碼

    Python并發(fā)之多進程的方法實例代碼

    這篇文章主要介紹了Python并發(fā)之多進程的方法實例代碼,文中也提到了進程與線程的共同點,需要的朋友跟隨腳本之家小編一起看看吧
    2018-08-08
  • python字符串連接的N種方式總結

    python字符串連接的N種方式總結

    python中有很多字符串連接方式,今天在寫代碼,順便總結一下,從最原始的字符串連接方式到字符串列表連接,大家感受下
    2014-09-09
  • python內置函數frozenset()的使用小結

    python內置函數frozenset()的使用小結

    本篇文章主要介紹了python內置函數frozenset()的使用小結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05

最新評論