# 鍊錶的基本組成就是乙個個node,每個node都包括兩部分內容,一部分是自身的data,另一部分是下乙個node的索引
# 每乙個鍊錶都有乙個head節點和乙個end節點,end節點的next為none
class node:
def __init__(self,init_data):
self.data = init_data
self.next = none
def get_data(self):
return self.data
def set_data(self,new_data):
self.data = new_data
def get_next(self):
return self.next
def set_next(self,new_next):
self.next = new_next
class unorderlist:
def __init__(self):
self.head = none
def is_empty(self):
return self.head == none
def add(self,item):
temp = node(item) # 建立新節點
temp.next = self.head # 將新節點鏈結之前的鍊錶
self.head = temp # 將新節點設為head
def size(self):
current = self.head
count = 0
while(current != none):
count += 1
current = current.get_next()
return count
def search(self,item):
current = self.head
found = false
while(current != none and not found):
if current.get_data() == item:
found = true
else:
current = current.get_next()
return found
def remove(self,item):
current = self.head
previous = none
found = false
while(not found):
if current.get_data() == item:
found = true
else:
previous = current
current = current.get_next()
if previous == none:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
class orderlist:
def __init__(self): # 無變化
self.head = none
def is_empty(self): # 無變化
return self.head == none
def add(self,item): # 變化最大的在這裡
current = self.head
previous = none
stop = false
while(current != none and not stop):
if current.get_data() > item:
stop = true
else:
previous = current
current = current.get_next()
temp = node(item)
if previous == none:
temp.set_next(self.head)
self.head = temp
else:
temp.set_next(current)
previous.set_next(temp)
def size(self): #無變化
current = self.head
count = 0
while(current != none):
current += 1
current = current.get_next()
return count
def search(self,item): #其實沒變化,stop可有可無
current = self.head
found = false
stop = false
while(current != none and not found and not stop):
if current.get_data() == item:
found = true
else:
if current.get_data() > item:
stop = true
else:
current = current.get_next()
return found
def remove(self,item): # 無變化
current = self.head
previous = none
found = false
while(not found):
if current.get_data() == item:
found = true
else:
previous = current
current = current.get_next()
if previous == none:
self.head = current.get_next()
else:
previous.set_next(current.get_next())
資料結構 鍊錶
鍊錶 what 就是一張鏈式儲存的表,是一種資料結構,是基礎,所以還是不要想有什麼用。具體呢?在c中就用結構體實現物件描述,然後通過函式來實現各個基本操作 c 則用類來表述,c中的結構體就可以看成c 中的類,然後通過類封裝各個操作步驟。這些操作實現後就需要 來測試,號稱demo,就是main函式裡面...
資料結構 鍊錶
鍊錶中的資料是以節點來表示的,每個結點的構成 元素 資料元素的映象 指標 指示後繼元素儲存位置 元素就是儲存資料的儲存單元,指標就是連線每個結點的位址資料。鍊錶的結點結構 data next data域 存放結點值的資料域 next域 存放結點的直接後繼的位址 位置 的指標域 鏈域 以 結點的序列 ...
資料結構 鍊錶
一般的建立線性鍊錶有兩種 1.正序法 需要三個指標,head作為頭指標,pre作為前乙個指標,cur作為當前指標用來建立空間 2.倒序法,利用指標的插入,只需要兩個指標,不斷的往頭指標後插入新空間,不過插入的越早,離頭指標越遠,也就越後面輸出 1.線性鍊錶的建立及查詢刪除 include inclu...