1、實現一個斐波拉契函數
F[n]=F[n-1]+Fn-2
要求輸入下標,返回對應值。
def A():
""""斐波那契,輸入下標值,輸出對應下標的值"""
global stl
num = input("請您輸入下標值:")
if int(num) > 2:
stl = [[],1, 1]
for i in range(3, int(num)+1):
stl.append(stl[-1] + stl[-2])
print(stl[int(num)])
2、已知二叉樹 前序:enHoiBrE
中序:HnioBerE
求: 後序:
HiBonEre
`3、實現一個自定義的雙向鏈表,要求支持增、刪;
class Node(object):
"""鏈表單節點實現"""
def __init__(self, item):
self.item = item # 元素域
self.next = None # 尾鏈接域
self.pre = None # 前驅指針
class DoubleLink:
"""雙向鏈表"""
def __init__(self, node=None):
self.__head = node
# head爲指向一個鏈表頭結點的p變量,接收一個參數node,node就是一個結點(第一個結點),當然也可以不傳參數,
# 當做一個空鏈表
# head設置成私有屬性,防止外部訪問,如果head被外部訪問並改動,則head之前指向的鏈表就丟失找不到了
def is_empty(self):
"""判斷鏈表是否爲空
:return 如果鏈表爲空, 返回真
"""
return self.__head is None
def length(self):
"""
:return: 鏈表長度
cur 遊標
count 結點計數
"""
cur = self.__head # 設置遊標指向第一個結點
count = 0 # 計數初始值爲:0
while cur is not None: # 如果遊標指向的結點不爲空則進入循環進行計數
count += 1 # 每次加一
cur = cur.next # 如果cur不爲空則讓遊標指向下一個結點
return count # 最後返回鏈表長度
def travel(self):
"""
遍歷鏈表
:return:打印鏈表所有元素
"""
cur = self.__head
while cur is not None:
print(cur.item, end=" ")
cur = cur.next
print()
def add(self, item):
"""
在鏈表頭部添加結點
:param item: 結點元素
:return:
"""
node = Node(item) # 創建一個結點對象
if self.is_empty(): # 如果原始鏈表爲空,直接讓head指向新結點就可以了
self.__head = node
else:
node.next = self.__head # 讓新結點的next指向原始的第一個結點
self.__head.pre = node # 讓原始結點的pre指向新節點
self.__head = node # 讓頭指針指向新加入的結點
def append(self, item):
"""
在鏈表的尾部追加結點
:param item:
:return:
"""
node = Node(item)
# 如果鏈表爲空則需要特殊處理
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next is not None:
cur = cur.next
cur.next = node # 讓原始結點的爲指針指向node
node.pre = cur # 讓node的前驅指針指向原始尾結點
def insert(self, pos, item):
"""
在指定位置插入元素
:param item:
:return:
"""
if pos <= 0: # 如果要插入的位置小於等於0,則相當於在頭部加入結點
self.add(item) # 直接調用已經寫好的方法
elif pos >= self.length(): # 如果要插入的位置大於等於鏈表的長度,則相當於在鏈表的尾部追加元素
self.append(item) # 直接調用寫好的方法
else: # 如果插入位置在鏈表中間則進行一下操作
cur = self.__head # 遊標指向第一個結點
count = 0 # 計數制0
while count < (pos - 1): # (pos - 1) cur需要停留在pos位置的前一個結點的位置
cur = cur.next
count += 1
# 當退出循環的時候cur指向pos的前一個位置
node = Node(item) # 創建新結點
# TODO 如果四個指針複製的順序出錯會造成死循環(如下:)
"""
cur.next = node # 將cur指向新結點]
node.pre = cur
node.next = cur.next # 將新結點指向cur的下一個結點
cur.next.pre = node # 讓cur的後一個結點的pre指向node
"""
node.pre = cur
node.next = cur.next # 將新結點指向cur的下一個結點
cur.next.pre = node # 讓cur的後一個結點的pre指向node
cur.next = node # 將cur指向新結點]
def remove(self, item):
"""刪除結點"""
cur = self.__head
while cur is not None: # 如果cur不爲空則進入循環
# 找到了要刪除的元素
if cur.item == item:
# 在頭部找到了元素
if cur == self.__head: # 如果找到的元素在頭結點上
self.__head = cur.next # 讓頭結點指向下一個結點,丟棄上個結點
if cur.next:
cur.next.pre = None # 將原始頭結點刪除後將下一個結點的pre指向空
else: # 找到的結點不在頭結點上,
cur.pre.next = cur.next
if cur.next:
cur.next.pre = cur.pre
return # 刪除後直接結束循環
cur = cur.next
def search(self, item):
"""
查找結點是否存在
:return 布爾型
"""
cur = self.__head
while cur is not None:
if cur.item == item:
return True # 如果找到了目標結點就返回true
cur = cur.next
return False # 如果整個循環結束也沒有找到目標結點,就返回false
if __name__ == '__main__':
link = DoubleLink()
print(link.length())
print(link.is_empty())
link.add(1)
link.travel()
link.append(6)
link.travel()
link.insert(1, 3)
link.travel()
print(link.length())
print(link.search(1))
link.remove(3)
link.travel()
link.remove(6)
link.remove(1)
print(link.length())
4、使用多線程遍歷打印第3題實現的鏈表。