創帆雲:python成為專業人士筆記--強烈建議收藏!每日持續更新!鍊錶是節點的集合,每個節點由乙個索引和乙個值組成,索引將所有節點串成了乙個序列。自定義鍊錶可用於實現更為複雜的資料結構,如列表、堆疊、佇列和關聯陣列等。
本例使用與內建list物件相同的許多方法實現了乙個鍊錶
#元素節點處理類
class node:
def __init__(self, val):
self.data = val
self.next = none
def getdata(self):
return self.data
def getnext(self):
return self.next
def setdata(self, val):
self.data = val
def setnext(self, val):
self.next = val
#鍊錶類
class linkedlist:
def __init__(self):
self.head = none
def isempty(self):
""" 檢查鍊錶是否為空 """
return self.head is none
def add(self, item):
""" 將物件新增到鍊錶中 """
new_node = node(item)
new_node.setnext(self.head)
self.head = new_node
def size(self):
"""返回鍊錶的長度"""
count = 0
current = self.head
while current is not none:
count += 1
current = current.getnext()
return count
def search(self, item):
""" 在鍊錶中搜尋專案。如果找到,返回true。如果沒有找到,返回false """
current = self.head
found = false
while current is not none and not found:
if current.getdata() is item:
found = true
else:
current = current.getnext()
return found
def remove(self, item):
""" 從鍊錶中刪除專案。如果在鍊錶中沒有找到專案,則引發valueerror """
current = self.head
previous = none
found = false
while current is not none and not found:
if current.getdata() is item:
found = true
else:
previous = current
current = current.getnext()
if found:
if previous is none:
self.head = current.getnext()
else:
previous.setnext(current.getnext())
else:
print('未找到值')
raise valueerror
def insert(self, position, item):
"""在指定的位置插入專案。如果指定的位置超出界限,則引發indexerror
"""if position > self.size() - 1:
print("index out of bounds.")
raise indexerror
current = self.head
previous = none
pos = 0
if position is 0:
self.add(item)
else:
new_node = node(item)
while pos < position:
pos += 1
previous = current
current = current.getnext()
previous.setnext(new_node)
new_node.setnext(current)
def index(self, item):
"""返回找到專案的索引。如果沒有找到專案,則返回none。
"""current = self.head
pos = 0
found = false
while current is not none and not found:
if current.getdata() is item:
found = true
else:
current = current.getnext()
pos += 1
if found:
pass
else:
pos = none
return pos
def pop(self, position=none):
"""如果沒有提供引數,則返回並刪除頭部的項。如果提供了位置,則返回並刪除該位 置的項。如果索引越界,則引發indexerror
"""if position > self.size():
print('index out of bounds')
raise indexerror
current = self.head
if position is none:
ret = current.getdata()
self.head = current.getnext()
else:
pos = 0
previous = none
while pos < position:
previous = current
current = current.getnext()
pos += 1
ret = current.getdata()
previous.setnext(current.getnext())
print(ret)
return ret
""" 將項追加到鍊錶的末尾 """
current = self.head
previous = none
pos = 0
length = self.size()
while pos < length:
previous = current
current = current.getnext()
pos += 1
new_node = node(item)
if previous is none:
new_node.setnext(current)
self.head = new_node
else:
previous.setnext(new_node)
def printlist(self):
"""列印鍊錶"""
current = self.head
while current is not none:
print(current.getdata())
current = current.getnext()
#函式呼叫
#使用函式過程非常類似於內建list列表
ll = linkedlist()
ll.add('l') #注意函式,頭部增加
ll.add('h')
ll.insert(1,'e') #指定位置新增
ll.printlist()
#輸出結果:
h el l
o
請仔細研究並在python3雲環境中測試上面的**, 如果參照以上**已經完整實現了鍊錶建立和操作方法,那麼完全可以基於此建立自定義堆疊、佇列等更複雜的資料結構
Python成為專業人士筆記 Sqlite3 模組
創帆雲 python成為專業人士筆記 強烈建議收藏!每日持續更新!sqlite3模組是由gerhard haring編寫的。要使用此模組,必須首先建立表示資料庫的連線物件。這裡的資料將儲存在example.db檔案中 import sqlite3 conn sqlite3.connect examp...
Python成為專業人士筆記 Set集合操作剖析
創帆雲 python成為專業人士筆記 強烈建議收藏!每日持續更新!與另乙個集合互相操作 intersection 交集 intersection 或 輸出 union 合併 union 輸出 difference 集合相減 difference 輸出 symmetric difference 對稱差...
Python成為專業人士筆記 位操作符
創帆雲 python成為專業人士筆記 強烈建議收藏!每日持續更新!按位操作符直接操作二進位制的字串,這些是最基本的操作,並直接由 處理器所支援。在處理裝置驅動程式 圖形底層 加密和網路通訊時,這些位操作是必需的。本文提供一些有用的知識和對應python的按位運算子示例 運算子將翻轉數字中的所有位,由...