這篇文章主要為大家詳細介紹了python資料結構之鍊錶的相關資料,具有一定的參考價值,有興趣的小夥伴們可以參考一下
資料結構是電腦科學必須掌握的一門學問,之前很多的教材都是用C語言實現鍊錶,因為c有指針,可以很方便的控制內存,很方便就實現鍊錶,其他的語言,則沒那麼方便,有很多都是用模擬鍊錶,不過這次,我不是用模擬鍊錶來實現,因為python是動態語言,可以直接把物件賦值給新的變數。
好了,在說我用python實作前,先簡單說說鍊錶吧。在我們儲存一大波資料時,我們很多時候是使用數組,但是當我們執行插入操作的時候就是非常麻煩,看下面的例子,有一堆資料1,2,3,5,6,7我們要在3和5之間插入4,如果用數組,我們會怎麼做?當然是將5之後的資料往後退一位,然後再插入4,這樣非常麻煩,但是如果用鍊錶,我就直接在3和5之間插入4就行,聽著就很方便。
那麼鍊錶的結構是怎麼樣的呢?顧名思義,鍊錶當然像鎖鏈一樣,由一節節節點連在一起,組成一條資料鏈。
鍊錶的節點的結構如下:
data為自訂的數據,next為下一個節點的位址。
鍊錶的結構為,head保存首位節點的位址:
#接下來我們來用python實作鍊錶
python實作鍊錶
首先,定義節點類別Node:
class Node: ''' data: 节点保存的数据 _next: 保存下一个节点对象 ''' def __init__(self, data, pnext=None): self.data = data self._next = pnext def __repr__(self): ''' 用来定义Node的字符输出, print为输出data ''' return str(self.data)
然後,定義鍊錶類別:
鍊錶要包含:
屬性:
鍊錶頭:head
鍊錶長度:length
方法:
判斷是否為空: isEmpty( )
def isEmpty(self): return (self.length == 0
增加一個節點(在鍊錶尾新增):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
刪除一個節點: delete()
#删除一个节点之后记得要把链表长度减一 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 #prev为保存前导节点 #node为保存当前节点 #当j与index相等时就 #相当于找到要删除的节点 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
修改一個節點: 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
尋找一個節點: 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
找出一個節點的索引: 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
插入一個節點: 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
以上就是鍊錶類別的要實作的方法。
執行的結果:
#接下來是完整程式碼:
# -*- 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
以上是詳細介紹python資料結構之鍊錶的詳細內容。更多資訊請關注PHP中文網其他相關文章!