基於Python單向連結串列實現尾部、任意位置新增,刪除
阿新 • • 發佈:2019-02-13
# coding = utf-8
# 建立節點類
class Node(object):
def __init__(self, data):
# 定義指向域
self.next = None
# 定義資料域
self.data = data
# 建立連結串列類
class LinkDict(object):
def __init__(self):
self._head = Node(None)
self._length = 0
# 尾部新增元素
def append(self, data):
# 構造新節點
new_code = Node(data)
# 查詢尾部節點並定義遊標指向頭節點,向尾部新增新節點
cur = self._head
# 迴圈遍歷
while cur.next is not None:
# 移動遊標
cur = cur.next
else:
# 向尾部新增新節點
cur.next = new_code
# 連結串列長度加1
self._length += 1
return self._length
# 迴圈遍歷連結串列元素
def traval(self):
cur = self._head
while cur.next is not None:
# 列印後邊節點
print(cur.next.data, "--->", end="")
# 遊標後移
cur = cur.next
else:
print("None:now is terminal")
# 任意位置新增元素
def insert(self, pos, data):
# 判斷插入點
if isinstance(pos, int):
if pos < 0:
link_dict.insert(0, data)
elif pos > self._length:
link_dict.append(data)
else:
# 構造新節點
new_code = Node(data)
# 構造遊標
cur = self._head
# 構造計數器
cnt = 0
while cur.next is not None:
if cnt == pos:
# 讓新節點有所指向
new_code.next = cur.next
# 讓與新節點有關的節點有所指向
cur.next = new_code
self._length += 1
return
else:
# 移動遊標
cur = cur.next
# 計數器加1
cnt += 1
else:
print("pos位置無效, 請確認")
exit()
def is_empty(self):
if self._length == 0:
print("連結串列為空")
return True
else:
return False
def remove(self, data):
if link_dict.is_empty():
return True
else:
cur = self._head
while cur.next is not None:
if cur.next.data == data:
# 刪除節點
cur.next = cur.next.next
self._length -= 1
return
else:
# 遊標移動
cur = cur.next
else:
print("data is not in link_data")
if __name__ == "__main__":
link_dict = LinkDict()
print("尾 部 添 加: ", end="")
for i in range(10):
link_dict.append(i)
link_dict.traval()
print("任意位置新增: ", end="")
link_dict.insert(-1, "a")
link_dict.insert(100, "z")
link_dict.insert(6, "x")
link_dict.traval()
print("刪 除 節 點: ", end="")
link_dict.remove("a")
link_dict.remove("x")
link_dict.remove("z")
# 建立節點類
class Node(object):
def __init__(self, data):
# 定義指向域
self.next = None
# 定義資料域
self.data = data
# 建立連結串列類
class LinkDict(object):
def __init__(self):
self._head = Node(None)
self._length = 0
# 尾部新增元素
def append(self, data):
# 構造新節點
new_code = Node(data)
# 查詢尾部節點並定義遊標指向頭節點,向尾部新增新節點
cur = self._head
# 迴圈遍歷
while cur.next is not None:
# 移動遊標
cur = cur.next
else:
# 向尾部新增新節點
cur.next = new_code
# 連結串列長度加1
self._length += 1
return self._length
# 迴圈遍歷連結串列元素
def traval(self):
cur = self._head
while cur.next is not None:
# 列印後邊節點
print(cur.next.data, "--->", end="")
# 遊標後移
cur = cur.next
else:
print("None:now is terminal")
# 任意位置新增元素
def insert(self, pos, data):
# 判斷插入點
if isinstance(pos, int):
if pos < 0:
link_dict.insert(0, data)
elif pos > self._length:
link_dict.append(data)
else:
# 構造新節點
new_code = Node(data)
# 構造遊標
cur = self._head
# 構造計數器
cnt = 0
while cur.next is not None:
if cnt == pos:
# 讓新節點有所指向
new_code.next = cur.next
# 讓與新節點有關的節點有所指向
cur.next = new_code
self._length += 1
return
else:
# 移動遊標
cur = cur.next
# 計數器加1
cnt += 1
else:
print("pos位置無效, 請確認")
exit()
def is_empty(self):
if self._length == 0:
print("連結串列為空")
return True
else:
return False
def remove(self, data):
if link_dict.is_empty():
return True
else:
cur = self._head
while cur.next is not None:
if cur.next.data == data:
# 刪除節點
cur.next = cur.next.next
self._length -= 1
return
else:
# 遊標移動
cur = cur.next
else:
print("data is not in link_data")
if __name__ == "__main__":
link_dict = LinkDict()
print("尾 部 添 加: ", end="")
for i in range(10):
link_dict.append(i)
link_dict.traval()
print("任意位置新增: ", end="")
link_dict.insert(-1, "a")
link_dict.insert(100, "z")
link_dict.insert(6, "x")
link_dict.traval()
print("刪 除 節 點: ", end="")
link_dict.remove("a")
link_dict.remove("x")
link_dict.remove("z")
link_dict.traval()
程式執行結果:
尾 部 添 加: 0 --->1 --->2 --->3 --->4 --->5 --->6 --->7 --->8 --->9 --->Node:now is terminal
任意位置新增: a --->0 --->1 --->2 --->3 --->4 --->x --->5 --->6 --->7 --->8 --->9 --->z --->Node:now is terminal
刪 除 節 點: 0 --->1 --->2 --->3 --->4 --->5 --->6 --->7 --->8 --->9 --->Node:now is terminal