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

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

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

數(shù)據(jù)結(jié)構(gòu)是計(jì)算機(jī)科學(xué)必須掌握的一門(mén)學(xué)問(wèn),之前很多的教材都是用C語(yǔ)言實(shí)現(xiàn)鏈表,因?yàn)閏有指針,可以很方便的控制內(nèi)存,很方便就實(shí)現(xiàn)鏈表,其他的語(yǔ)言,則沒(méi)那么方便,有很多都是用模擬鏈表,不過(guò)這次,我不是用模擬鏈表來(lái)實(shí)現(xiàn),因?yàn)閜ython是動(dòng)態(tài)語(yǔ)言,可以直接把對(duì)象賦值給新的變量。

好了,在說(shuō)我用python實(shí)現(xiàn)前,先簡(jiǎn)單說(shuō)說(shuō)鏈表吧。在我們存儲(chǔ)一大波數(shù)據(jù)時(shí),我們很多時(shí)候是使用數(shù)組,但是當(dāng)我們執(zhí)行插入操作的時(shí)候就是非常麻煩,看下面的例子,有一堆數(shù)據(jù)1,2,3,5,6,7我們要在3和5之間插入4,如果用數(shù)組,我們會(huì)怎么做?當(dāng)然是將5之后的數(shù)據(jù)往后退一位,然后再插入4,這樣非常麻煩,但是如果用鏈表,我就直接在3和5之間插入4就行,聽(tīng)著就很方便。

那么鏈表的結(jié)構(gòu)是怎么樣的呢?顧名思義,鏈表當(dāng)然像鎖鏈一樣,由一節(jié)節(jié)節(jié)點(diǎn)連在一起,組成一條數(shù)據(jù)鏈。

鏈表的節(jié)點(diǎn)的結(jié)構(gòu)如下:

data為自定義的數(shù)據(jù),next為下一個(gè)節(jié)點(diǎn)的地址。

鏈表的結(jié)構(gòu)為,head保存首位節(jié)點(diǎn)的地址:

接下來(lái)我們來(lái)用python實(shí)現(xiàn)鏈表

python實(shí)現(xiàn)鏈表

首先,定義節(jié)點(diǎn)類Node:

class Node:
  '''
  data: 節(jié)點(diǎn)保存的數(shù)據(jù)
  _next: 保存下一個(gè)節(jié)點(diǎn)對(duì)象
  '''
  def __init__(self, data, pnext=None):
    self.data = data
    self._next = pnext

  def __repr__(self):
    '''
    用來(lái)定義Node的字符輸出,
    print為輸出data
    '''
    return str(self.data)

然后,定義鏈表類:

鏈表要包括:

屬性:

鏈表頭:head

鏈表長(zhǎng)度:length

方法:

判斷是否為空: isEmpty()

def isEmpty(self):
  return (self.length == 0

增加一個(gè)節(jié)點(diǎn)(在鏈表尾添加): append()

def append(self, dataOrNode):
  item = None
  if isinstance(dataOrNode, Node):
    item = dataOrNode
  else:
    item = Node(dataOrNode)

  if not self.head:
    self.head = item
    self.length += 1

  else:
    node = self.head
    while node._next:
      node = node._next
    node._next = item
    self.length += 1

刪除一個(gè)節(jié)點(diǎn): delete()

#刪除一個(gè)節(jié)點(diǎn)之后記得要把鏈表長(zhǎng)度減一
def delete(self, index):
  if self.isEmpty():
    print "this chain table is empty."
    return

  if index < 0 or index >= self.length:
    print 'error: out of index'
    return
  #要注意刪除第一個(gè)節(jié)點(diǎn)的情況
  #如果有空的頭節(jié)點(diǎn)就不用這樣
  #但是我不喜歡弄頭節(jié)點(diǎn)
  if index == 0:
    self.head = self.head._next
    self.length -= 1
    return

  #prev為保存前導(dǎo)節(jié)點(diǎn)
  #node為保存當(dāng)前節(jié)點(diǎn)
  #當(dāng)j與index相等時(shí)就
  #相當(dāng)于找到要?jiǎng)h除的節(jié)點(diǎn)
  j = 0
  node = self.head
  prev = self.head
  while node._next and j < index:
    prev = node
    node = node._next
    j += 1

  if j == index:
    prev._next = node._next
    self.length -= 1

修改一個(gè)節(jié)點(diǎn): update()

def update(self, index, data):
  if self.isEmpty() or index < 0 or index >= self.length:
    print 'error: out of index'
    return
  j = 0
  node = self.head
  while node._next and j < index:
    node = node._next
    j += 1

  if j == index:
    node.data = data

查找一個(gè)節(jié)點(diǎn): getItem()

def getItem(self, index):
  if self.isEmpty() or index < 0 or index >= self.length:
    print "error: out of index"
    return
  j = 0
  node = self.head
  while node._next and j < index:
    node = node._next
    j += 1

  return node.data

查找一個(gè)節(jié)點(diǎn)的索引: getIndex()

def getIndex(self, data):
  j = 0
  if self.isEmpty():
    print "this chain table is empty"
    return
  node = self.head
  while node:
    if node.data == data:
      return j
    node = node._next
    j += 1

  if j == self.length:
    print "%s not found" % str(data)
    return

插入一個(gè)節(jié)點(diǎn): insert()

def insert(self, index, dataOrNode):
  if self.isEmpty():
    print "this chain tabale is empty"
    return

  if index < 0 or index >= self.length:
    print "error: out of index"
    return

  item = None
  if isinstance(dataOrNode, Node):
    item = dataOrNode
  else:
    item = Node(dataOrNode)

  if index == 0:
    item._next = self.head
    self.head = item
    self.length += 1
    return

  j = 0
  node = self.head
  prev = self.head
  while node._next and j < index:
    prev = node
    node = node._next
    j += 1

  if j == index:
    item._next = node
    prev._next = item
    self.length += 1

清空鏈表: clear()

def clear(self):
  self.head = None
  self.length = 0

以上就是鏈表類的要實(shí)現(xiàn)的方法。

執(zhí)行的結(jié)果:

接下來(lái)是完整代碼:

# -*- coding:utf8 -*-
#/usr/bin/env python

class Node(object):
 def __init__(self, data, pnext = None):
  self.data = data
  self._next = pnext

 def __repr__(self):
  return str(self.data)

class ChainTable(object):
 def __init__(self):
  self.head = None
  self.length = 0

 def isEmpty(self):
  return (self.length == 0)

 def append(self, dataOrNode):
  item = None
  if isinstance(dataOrNode, Node):
   item = dataOrNode
  else:
   item = Node(dataOrNode)

  if not self.head:
   self.head = item
   self.length += 1

  else:
   node = self.head
   while node._next:
    node = node._next
   node._next = item
   self.length += 1

 def delete(self, index):
  if self.isEmpty():
   print "this chain table is empty."
   return

  if index < 0 or index >= self.length:
   print 'error: out of index'
   return

  if index == 0:
   self.head = self.head._next
   self.length -= 1
   return

  j = 0
  node = self.head
  prev = self.head
  while node._next and j < index:
   prev = node
   node = node._next
   j += 1

  if j == index:
   prev._next = node._next
   self.length -= 1

 def insert(self, index, dataOrNode):
  if self.isEmpty():
   print "this chain tabale is empty"
   return

  if index < 0 or index >= self.length:
   print "error: out of index"
   return

  item = None
  if isinstance(dataOrNode, Node):
   item = dataOrNode
  else:
   item = Node(dataOrNode)

  if index == 0:
   item._next = self.head
   self.head = item
   self.length += 1
   return

  j = 0
  node = self.head
  prev = self.head
  while node._next and j < index:
   prev = node
   node = node._next
   j += 1

  if j == index:
   item._next = node
   prev._next = item
   self.length += 1

 def update(self, index, data):
  if self.isEmpty() or index < 0 or index >= self.length:
   print 'error: out of index'
   return
  j = 0
  node = self.head
  while node._next and j < index:
   node = node._next
   j += 1

  if j == index:
   node.data = data

 def getItem(self, index):
  if self.isEmpty() or index < 0 or index >= self.length:
   print "error: out of index"
   return
  j = 0
  node = self.head
  while node._next and j < index:
   node = node._next
   j += 1

  return node.data


 def getIndex(self, data):
  j = 0
  if self.isEmpty():
   print "this chain table is empty"
   return
  node = self.head
  while node:
   if node.data == data:
    return j
   node = node._next
   j += 1

  if j == self.length:
   print "%s not found" % str(data)
   return

 def clear(self):
  self.head = None
  self.length = 0

 def __repr__(self):
  if self.isEmpty():
   return "empty chain table"
  node = self.head
  nlist = ''
  while node:
   nlist += str(node.data) + ' '
   node = node._next
  return nlist

 def __getitem__(self, ind):
  if self.isEmpty() or ind < 0 or ind >= self.length:
   print "error: out of index"
   return
  return self.getItem(ind)

 def __setitem__(self, ind, val):
  if self.isEmpty() or ind < 0 or ind >= self.length:
   print "error: out of index"
   return
  self.update(ind, val)

 def __len__(self):
  return self.length

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

相關(guān)文章

  • tensorflow實(shí)現(xiàn)圖像的裁剪和填充方法

    tensorflow實(shí)現(xiàn)圖像的裁剪和填充方法

    今天小編就為大家分享一篇tensorflow實(shí)現(xiàn)圖像的裁剪和填充方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例

    python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例

    今天小編就為大家分享一篇python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • python實(shí)現(xiàn)超級(jí)瑪麗游戲

    python實(shí)現(xiàn)超級(jí)瑪麗游戲

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超級(jí)瑪麗游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • 如何利用python腳本自動(dòng)部署k8s

    如何利用python腳本自動(dòng)部署k8s

    這篇文章主要介紹了利用python腳本自動(dòng)部署k8s的方法,本文通過(guò)腳本代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • Djang的model創(chuàng)建的字段和參數(shù)詳解

    Djang的model創(chuàng)建的字段和參數(shù)詳解

    這篇文章主要介紹了Djang的model創(chuàng)建的字段和參數(shù)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python實(shí)現(xiàn)簡(jiǎn)單的獲取圖片爬蟲(chóng)功能示例

    Python實(shí)現(xiàn)簡(jiǎn)單的獲取圖片爬蟲(chóng)功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單的獲取圖片爬蟲(chóng)功能,涉及Python使用urllib模塊及正則模塊操作頁(yè)面元素獲取圖片的相關(guān)技巧,需要的朋友可以參考下
    2017-07-07
  • python之Flask實(shí)現(xiàn)簡(jiǎn)單登錄功能的示例代碼

    python之Flask實(shí)現(xiàn)簡(jiǎn)單登錄功能的示例代碼

    這篇文章主要介紹了python之Flask實(shí)現(xiàn)簡(jiǎn)單登錄功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • python os用法總結(jié)

    python os用法總結(jié)

    本篇文章給大家分享了關(guān)于python os用法的相關(guān)總結(jié)性內(nèi)容,對(duì)此有學(xué)習(xí)需要朋友參考下吧。
    2018-06-06
  • 關(guān)于Python中的if __name__ == __main__詳情

    關(guān)于Python中的if __name__ == __main__詳情

    在學(xué)習(xí)Python的過(guò)程中發(fā)現(xiàn)即使把if __name__ == ‘__main__’ 去掉,程序還是照樣運(yùn)行。很多小伙伴只知道是這么用的,也沒(méi)有深究具體的作用。這篇文字就來(lái)介紹一下Python中的if __name__ == ‘__main__’的作用,需要的朋友參考下文
    2021-09-09
  • Python實(shí)現(xiàn)的讀寫(xiě)json文件功能示例

    Python實(shí)現(xiàn)的讀寫(xiě)json文件功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的讀寫(xiě)json文件功能,結(jié)合實(shí)例形式分析了Python針對(duì)json文件進(jìn)行讀寫(xiě)的常見(jiàn)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2018-06-06

最新評(píng)論

河曲县| 海林市| 惠水县| 崇州市| 霞浦县| 闸北区| 花莲县| 鄂伦春自治旗| 宜阳县| 筠连县| 上林县| 全州县| 寿宁县| 新野县| 怀柔区| 泸定县| 台中县| 汝阳县| 大洼县| 信丰县| 汾西县| 大方县| 尉犁县| 望谟县| 涞水县| 贵南县| 弥勒县| 珠海市| 永仁县| 砚山县| 吉安市| 蚌埠市| 鄯善县| 迭部县| 灌阳县| 宣恩县| 沁阳市| 德州市| 河源市| 邯郸县| 若羌县|