class node(object):
"""data:資料段
next:指標段
"""def __init__(self, value, next=node):
self.value = value
self.next = next
def __repr__(self):
"""用來定義node的字元輸出
print 為輸出data
"""return str(self.data)
class linkedlist(object):
def isempty(self):
return self.length == 0
item = node
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 space")
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
def update(self, index, data):
if self.isempty() or index < 0 or index >= self.length:
print("error! out of space")
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 space")
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 insert(self, index, dataornode):
if self.isempty():
print("this chain table is empty")
return
if index < 0 or index >= self.length:
print("error: out of space")
return
item = node
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 space")
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 space")
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 = node
self.length = 0
基本資料結構 鍊錶
鍊錶 鍊錶與陣列的區別是鍊錶中的元素順序是有各物件中的指標決定的,相鄰元素之間在物理記憶體上不一定相鄰。採用鍊錶可以靈活地表示動態集合。鍊錶有單鏈表和雙鏈表及迴圈鍊錶。書中著重介紹了雙鏈表的概念及操作,雙鏈表l的每乙個元素是乙個物件,每個物件包含乙個關鍵字和兩個指標 next和prev。鍊錶的操作包...
基本資料結構 鍊錶
談到鍊錶之前,先說一下線性表。線性表是最基本 最簡單 也是最常用的一種資料結構。線性表中資料元素之間的關係是一對一的關係,即除了第乙個和最後乙個資料元素之外,其它資料元素都是首尾相接的。線性表有兩種儲存方式,一種是順序儲存結構,另一種是鏈式儲存結構。順序儲存結構就是兩個相鄰的元素在記憶體中也是相鄰的...
Python 基本資料結構
列表是python裡的內建的型別之一,可以新增和刪除其中的元素。如 role sam 33 dean 37 可以列表中包含列表,同一列表中包含不同型別的資料。下面介紹列表的一些通用操作,關於list的其他用法,可以通過help list 檢視。a 2,13,12,41,17,23 a 1 列表從0開...