雙向連結清單定義
雙向連結清單(Double Linked List)是一種更複雜的連結清單,每個節點除了包含元素域,還包含兩個連結:一個指向前一個節點,當此節點為第一個節點時,指向空值;另一個指向下一個節點,當此節點為最後一個節點時,指向空值。
節點示意圖
- 表元素域
用來存放具體的資料。elem
- 連結域
用來存放上一個節點的位置(python中的辨別)prev
- 連結域
用來存放下一個節點的位置(python中的辨別)next
雙向連結清單示意圖
雙向連結清單的基本操作
-
判斷連結清單是否為空is_empty()
-
連結清單長度length
-
周遊整個連結清單,列印元素travel()
-
在連結清單頭部添加元素add(item)
-
在連結清單尾部添加元素append(item)
-
在指定位置插入元素insert(pos, item)
-
删除元素remove(item)
-
清空連結清單clear()
-
判斷元素是否存在is_contain(item)
Python 代碼實作
# 節點代碼實作
class Node(object):
"""雙向連結清單節點"""
def __init__(self, item):
self.item = item
self.next = None
self.prev = None
# 雙向連結清單代碼實作
class DoubleLinkList(object):
"""雙向連結清單"""
def __init__(self):
self._head = None
def is_empty(self):
"""判斷連結清單是否為空"""
return self._head is None
@property
def length(self):
"""傳回連結清單的長度"""
cur = self._head
count = 0
while cur is not None :
count += 1
cur = cur.next
return count
def travel(self):
"""周遊連結清單"""
cur = self._head
while cur is not None:
print(cur.item)
cur = cur.next
print("")
def add(self, item):
"""頭部插入元素"""
node = Node(item)
if self.is_empty():
# 如果是空連結清單,将_head指向node
self._head = node
else:
# 将node的next指向_head的頭節點
node.next = self._head
# 将_head的頭節點的prev指向node
self._head.prev = node
# 将_head 指向node
self._head = node
def append(self, item):
"""尾部插入元素"""
node = Node(item)
if self.is_empty():
# 如果是空連結清單,将_head指向node
self._head = node
else:
# 移動到連結清單尾部
cur = self._head
while cur.next is not None:
cur = cur.next
# 将尾節點cur的next指向node
cur.next = node
# 将node的prev指向cur
node.prev = cur
def is_contain(self, item):
"""查找元素是否存在"""
cur = self._head
while cur is not None:
if cur.item == item:
return True
cur = cur.next
return False
def insert(self, pos, item):
"""在指定位置添加節點"""
if pos <= 0:
self.add(item)
elif pos > (self.length-1):
self.append(item)
else:
node = Node(item)
cur = self._head
count = 0
# 移動到指定位置的前一個位置
while count < (pos-1):
count += 1
cur = cur.next
# 将node的prev指向cur
node.prev = cur
# 将node的next指向cur的下一個節點
node.next = cur.next
# 将cur的下一個節點的prev指向node
cur.next.prev = node
# 将cur的next指向node
cur.next = node
def remove(self, item):
"""删除元素"""
if self.is_empty():
return
else:
cur = self._head
if cur.item == item:
# 如果首節點的元素即是要删除的元素
if cur.next is None:
# 如果連結清單隻有這一個節點
self._head = None
else:
# 将第二個節點的prev設定為None
cur.next.prev = None
# 将_head指向第二個節點
self._head = cur.next
return
while cur is not None:
if cur.item == item:
# 将cur的前一個節點的next指向cur的後一個節點
cur.prev.next = cur.next
# 将cur的後一個節點的prev指向cur的前一個節點
cur.next.prev = cur.prev
break
cur = cur.next
def clear(self):
"""清空連結清單"""
self._head = None
def __len__(self):
"""可以用len()方法擷取連結清單長度"""
return self.length
def __iter__(self):
"""可以使用循環周遊連結清單"""
cur = self._head
while cur is not None:
value = cur.item
cur = cur.next
yield value
def __contains__(self, item):
"""可以用in判斷元素是否在連結清單中"""
cur = self._head
while cur is not None:
if cur.item == item:
return True
cur = cur.next
return False
# 測試資料
if __name__ == "__main__":
print("------建立連結清單------")
dl_list = DoubleLinkList()
dl_list.add(1)
dl_list.add(2)
dl_list.append(3)
dl_list.insert(2, 4)
dl_list.insert(4, 5)
dl_list.insert(0, 6)
print("length:",len(dl_list))
dl_list.travel()
print(dl_list.is_contain(3))
print(dl_list.is_contain(8))
print(3 in dl_list)
print(8 in dl_list)
dl_list.remove(1)
print("length:",len(dl_list))
dl_list.travel()
print("------循環周遊------")
for i in dl_list:
print(i)
# 輸出結果
------建立連結清單------
length: 6
6
2
1
4
3
5
True
False
True
False
length: 5
6
2
4
3
5
------循環周遊------
6
2
4
3
5
算法分析
操作 | 複雜度 |
---|---|
通路元素 | O ( n ) O(n) O(n) |
在頭部插入/删除 | O ( 1 ) O(1) O(1) |
在尾部插入/删除 | O ( n ) O(n) O(n) |
在中間插入/删除 | O ( n ) O(n) O(n) |
聯系我們
個人部落格網站:http://www.bling2.cn/
Github位址:https://github.com/lb971216008/Use-Python-to-Achieve
知乎專欄:https://zhuanlan.zhihu.com/Use-Python-to-Achieve
小專欄:https://xiaozhuanlan.com/Use-Python-to-Achieve
部落格園:https://www.cnblogs.com/Use-Python-to-Achieve