python 實現單鏈表

數據結構是計算機存儲、組織數據的方式,結構不一樣那麼數據的檢索方式和效率都不同node

經常使用的數據結構有  數組 、棧 、隊列 、鏈表 、樹、堆數組

今天講下單鏈表,單鏈表是一種鏈式存取的數據結構, 跟順序鏈表徹底部同樣 是一種非順序結構存儲數據結構

單鏈表是結點表示數據,結點包括數據和後繼元素構成(用來存放下一個節點的位置)app

鏈表的缺點失去順序表讀取的優勢,增長告終點的地址,空間開銷比較大,但比順序存儲空間的使用要相對靈活spa

鏈表主要操做主要是遍歷操做,效率下降code

單鏈表的表現形式,這種結構不像順序結構連續存儲,是一種非連續,非順序的存儲結構  例如如下blog

單鏈表代碼實現隊列

 1 class Node(object):
 2     def __init__(self,elem):
 3         self.elem = elem
 4         self.next = None
 5 
 6 class SingleLinkList(object):
 7     """單向列表"""
 8     def __init__(self):
 9         self.__head = None
10 
11     def is_empty(self):
12         return self.__head is None
13 
14     def get_length(self):
15         """遍歷整個列表"""
16         curr_node = self.__head
17         count = 0
18         while curr_node.next is not None:
19             count += 1
20             curr_node = curr_node.next
21         count += 1
22         return count
23 
24     def add(self,item):
25         """頭部添加"""
26         node = Node(item)
27         node.next = self.__head
28         self.__head = node
29 
30     def append(self,item):
31         node = Node(item)
32         if self.is_empty():
33             self.__head = node
34         else:
35             curr_node = self.__head
36             while curr_node.next is not None:
37                 curr_node = curr_node.next
38             curr_node.next = node
39 
40     def insert(self,pos,item):
41         if pos <=0:
42             self.add(item)
43         elif pos >(self.length()-1):
44             self.append(item)
45         else:
46             pre = self.__head
47             count = 0
48             while count < (pos-1):
49                 count +=1
50                 pre = pre.next
51             #循環退出後 pre指向pos-1
52             node = Node(item)
53             node.next = pre.next
54             pre.next = node
55 
56     def remove(self,item):
57         """刪除數據"""
58         curr_node = self.__head
59         pre = None
60         while curr_node is not None:
61             if curr_node.elem == item:
62                 #先判斷是不是頭結點
63                 if curr_node == self.__head:
64                     self.__head = curr_node.next
65                 else:
66                     pre.next = curr_node.next
67                 break
68             else:
69                 pre = curr_node
70                 curr_node = curr_node.next
71 
72     def elem_travel(self):
73         """遍歷整個列表"""
74         if self.is_empty():
75             return None
76         curr_node = self.__head
77         while curr_node.next is not None:
78             print(curr_node.elem, " ")
79             curr_node = curr_node.next
80         print(curr_node.elem, " ")
81 
82 if __name__ == "__main__":
83     linkList = SingleLinkList()
84     print("當前鏈表是否爲空:",linkList.is_empty())    #True
85     linkList.add(9)
86     linkList.add(12)
87     linkList.append(4)
88     linkList.append(8)
89     print("當前鏈表是否爲空:", linkList.is_empty())  # False
90     print("當前鏈表長度:",linkList.get_length()) # 4
91     linkList.remove(4)
92     print("當前鏈表長度:", linkList.get_length()) # 3
93     print("當前鏈表數據:")
94     linkList.elem_travel()  # 9 12 8

運行結果:rem

相關文章
相關標籤/搜索