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

python如何對鏈表操作

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

鏈表

鏈表(linked list)是由一組被稱為結(jié)點(diǎn)的數(shù)據(jù)元素組成的數(shù)據(jù)結(jié)構(gòu),每個結(jié)點(diǎn)都包含結(jié)點(diǎn)本身的信息和指向下一個結(jié)點(diǎn)的地址。
由于每個結(jié)點(diǎn)都包含了可以鏈接起來的地址信息,所以用一個變量就能夠訪問整個結(jié)點(diǎn)序列。
也就是說,結(jié)點(diǎn)包含兩部分信息:一部分用于存儲數(shù)據(jù)元素的值,稱為信息域;另一部分用于存儲下一個數(shù)據(jù)元素地址的指針,稱為指針域。鏈表中的第一個結(jié)點(diǎn)的地址存儲在一個單獨(dú)的結(jié)點(diǎn)中,稱為頭結(jié)點(diǎn)或首結(jié)點(diǎn)。鏈表中的最后一個結(jié)點(diǎn)沒有后繼元素,其指針域?yàn)榭铡?/p>

代碼

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

  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é)點(diǎn)'
    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é)點(diǎn)'
    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é)點(diǎn)'
    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如何對鏈表操作的詳細(xì)內(nèi)容,更多關(guān)于python 鏈表操作的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 如何利用Python連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)儲存

    如何利用Python連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)儲存

    當(dāng)我們學(xué)習(xí)了mysql數(shù)據(jù)庫后,我們會想著該如何將python和mysql結(jié)合起來運(yùn)用,下面這篇文章主要給大家介紹了關(guān)于如何利用Python連接MySQL數(shù)據(jù)庫實(shí)現(xiàn)數(shù)據(jù)儲存的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • 用Python快速讀取Excel數(shù)據(jù)

    用Python快速讀取Excel數(shù)據(jù)

    嘿,想學(xué)會用Python快速讀取Excel數(shù)據(jù)嗎?不用擔(dān)心,這個指南將帶你輕松掌握這項技能,讓我們一起開始吧!
    2023-12-12
  • Python使用functools模塊中的partial函數(shù)生成偏函數(shù)

    Python使用functools模塊中的partial函數(shù)生成偏函數(shù)

    所謂偏函數(shù)即是規(guī)定了固定參數(shù)的函數(shù),在函數(shù)式編程中我們經(jīng)??梢杂玫?這里我們就來看一下Python使用functools模塊中的partial函數(shù)生成偏函數(shù)的方法
    2016-07-07
  • Python?argparse庫的基本使用步驟

    Python?argparse庫的基本使用步驟

    argparse庫是python下的一個命令行參數(shù)管理庫,支持int、str、float、bool、數(shù)組等5種基本數(shù)據(jù)類型,這篇文章主要介紹了Python?argparse庫的基本使用,需要的朋友可以參考下
    2022-07-07
  • nonebot插件之chatgpt使用詳解

    nonebot插件之chatgpt使用詳解

    這篇文章主要為大家介紹了nonebot插件之chatgpt使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • python字典根據(jù)key排序的實(shí)現(xiàn)

    python字典根據(jù)key排序的實(shí)現(xiàn)

    本文主要介紹了python字典根據(jù)key排序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-01-01
  • Python爬蟲實(shí)現(xiàn)驗(yàn)證碼登錄代碼實(shí)例

    Python爬蟲實(shí)現(xiàn)驗(yàn)證碼登錄代碼實(shí)例

    這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)驗(yàn)證碼登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 深入了解Python iter() 方法的用法

    深入了解Python iter() 方法的用法

    這篇文章主要介紹了深入了解Python iter() 方法的知識,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-07-07
  • Python實(shí)現(xiàn)打乒乓小游戲

    Python實(shí)現(xiàn)打乒乓小游戲

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)打乒乓小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Python3 Random模塊代碼詳解

    Python3 Random模塊代碼詳解

    這篇文章主要介紹了Python3 Random模塊代碼詳解,具有一定參考價值,需要的朋友可以了解下。
    2017-12-12

最新評論

元江| 米泉市| 阳谷县| 许昌县| 修文县| 昭平县| 固安县| 洞口县| 拜泉县| 肇州县| 辉南县| 郁南县| 渭南市| 淄博市| 阿图什市| 科技| 固阳县| 车致| 定州市| 永宁县| 沂源县| 新密市| 巍山| 北安市| 循化| 敦化市| 福建省| 安新县| 林州市| 神木县| 皋兰县| 西乌| 丽水市| 和硕县| 延庆县| 潜山县| 南岸区| 邢台县| 遂川县| 建始县| 盈江县|