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

Python數(shù)據(jù)結(jié)構(gòu)之單鏈表詳解

 更新時(shí)間:2017年09月12日 10:05:01   作者:方程同調(diào)士  
這篇文章主要為大家詳細(xì)介紹了Python數(shù)據(jù)結(jié)構(gòu)之單鏈表的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Python數(shù)據(jù)結(jié)構(gòu)之單鏈表的具體代碼,供大家參考,具體內(nèi)容如下

# 節(jié)點(diǎn)類
class Node():
  __slots__=['_item','_next'] # 限定Node實(shí)例的屬性
  def __init__(self,item):
    self._item = item
    self._next = None # Node的指針部分默認(rèn)指向None
  def getItem(self):
    return self._item
  def getNext(self):
    return self._next
  def setItem(self,newitem):
    self._item = newitem
  def setNext(self,newnext):
    self._next=newnext

# 單鏈表
class SingleLinkedList():
  def __init__(self):
    self._head = None #初始化鏈表為空 始終指向鏈表的頭部
    self._size = 0 # 鏈表大小

  # 返回鏈表的大小
  def size(self):
    current = self._head
    count = 0
    while current != None:
      count += 1
      current = current.getNext()
    return count

  # 遍歷鏈表
  def travel(self):
    current = self._head
    while current != None:
      print(current.getItem())
      current = current.getNext()
  # 檢查鏈表是否為空
  def isEmpty(self):
    return self._head == None

  # 在鏈表前端添加元素
  def add(self,item):
    temp = Node(item) # 創(chuàng)建新的節(jié)點(diǎn)
    temp.setNext(self._head) # 新創(chuàng)建的next指針指向_head
    self._head = temp # _head指向新創(chuàng)建的指針

  # 在鏈表尾部添加元素
  def append(self,item):
    temp = Node(item)
    if self.isEmpty():
      self._head = temp # 若為空表就直接插入
    else:
      current = self._head
      while current.getNext() != None:
        current = current.getNext() # 遍歷列表
      current.setNext(temp) # 此時(shí)current為鏈表最后的元素,在末尾插入

  # 檢索元素是否在鏈表中
  def search(self,item):
    current = self._head
    founditem = False
    while current != None and not founditem:
      if current.getItem() == item:
        founditem = True
      else:
        current = current.getNext()
    return founditem

  # 索引元素在表中的位置
  def index(self,item):
    current = self._head
    count = 0
    found = None
    while current != None and not found:
      count += 1
      if current.getItem() == item:
        found = True
      else:
        current = current.getNext()
    if found:
      return count
    else:
      return -1 # 返回-1表示不存在

  # 刪除表中的某項(xiàng)元素
  def remove(self,item):
    current = self._head
    pre = None
    while current!=None:
      if current.getItem() == item:
        if not pre:
          self._head = current.getNext()
        else:
          pre.setNext(current.getNext())
        break
      else:
        pre = current
        current = current.getNext()

  # 在鏈表任意位置插入元素
  def insert(self,pos,item):
    if pos <= 1:
      self.add(item)
    elif pos > self.size():
      self.append(item)
    else:
      temp = Node(item)
      count = 1
      pre = None
      current = self._head
      while count < pos:
        count += 1
        pre = current
        current = current.getNext()
      pre.setNext(temp)
      temp.setNext(current)


if __name__=='__main__':
  a=SingleLinkedList()
  for i in range(1,10):
    a.append(i)
  print('鏈表的大小',a.size())
  a.travel()
  print(a.search(6))
  print(a.index(5))
  a.remove(4)
  a.travel()
  a.insert(4,100)
  a.travel()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • python類的實(shí)例化問(wèn)題解決

    python類的實(shí)例化問(wèn)題解決

    這篇文章主要介紹了python類的實(shí)例化問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python生成隨機(jī)mac地址的方法

    python生成隨機(jī)mac地址的方法

    這篇文章主要介紹了python生成隨機(jī)mac地址的方法,涉及Python操作隨機(jī)字符串的技巧,需要的朋友可以參考下
    2015-03-03
  • Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格

    Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格

    這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)Excel自動(dòng)分組合并單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-02-02
  • Python中綁定與未綁定的類方法用法分析

    Python中綁定與未綁定的類方法用法分析

    這篇文章主要介紹了Python中綁定與未綁定的類方法用法,結(jié)合實(shí)例形式分析了未綁定的類方法與綁定的實(shí)例方法相關(guān)使用技巧,需要的朋友可以參考下
    2016-04-04
  • python 用遞歸實(shí)現(xiàn)通用爬蟲(chóng)解析器

    python 用遞歸實(shí)現(xiàn)通用爬蟲(chóng)解析器

    這篇文章主要介紹了python 用遞歸實(shí)現(xiàn)通用爬蟲(chóng)解析器的方法,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • Python 操作 MongoDB 講解詳細(xì)

    Python 操作 MongoDB 講解詳細(xì)

    MongoDB是一款開(kāi)源的、基于分布式的、面向文檔存儲(chǔ)的非關(guān)系型數(shù)據(jù)庫(kù)。擁有高性能、高可用,易擴(kuò)展的優(yōu)點(diǎn),并且支持豐富的查詢語(yǔ)言來(lái)支持讀寫操作以及更復(fù)雜的查詢等。接下來(lái)我們來(lái)看一下如何使用Python操作MongoDB數(shù)據(jù)庫(kù)
    2021-09-09
  • 簡(jiǎn)述Python2與Python3的不同點(diǎn)

    簡(jiǎn)述Python2與Python3的不同點(diǎn)

    在Python2和Python3中都提供print()方法來(lái)打印信息,但兩個(gè)版本間的print稍微有差異。下面通過(guò)本文給大家介紹Python2與Python3的不同點(diǎn),需要的朋友參考下
    2018-01-01
  • 關(guān)于python處理大型json文件的方法

    關(guān)于python處理大型json文件的方法

    這篇文章主要介紹了python處理大型json文件的方法,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • python xlsxwriter創(chuàng)建excel圖表的方法

    python xlsxwriter創(chuàng)建excel圖表的方法

    這篇文章主要為大家詳細(xì)介紹了python xlsxwriter創(chuàng)建excel圖表的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Python使用正則表達(dá)式報(bào)錯(cuò):nothing?to?repeat?at?position?0的解決方案

    Python使用正則表達(dá)式報(bào)錯(cuò):nothing?to?repeat?at?position?0的解決方案

    今天在使用python 正則模塊匹配字符串時(shí)遇到了這個(gè)問(wèn)題,分享給大家,這篇文章主要給大家介紹了關(guān)于Python使用正則表達(dá)式報(bào)錯(cuò)nothing?to?repeat?at?position?0的解決方案,需要的朋友可以參考下
    2023-03-03

最新評(píng)論