Liên biểu cơ sở ( Linked list )
Định nghĩa
Liên biểu: Thị nhất chủng tuyến tính biểu, đãn thị tịnh bất hội án tuyến tính đích thuận tự tồn trữ sổ cư, nhi thị tại mỗi nhất cá tiết điểm lí tồn đáo hạ nhất cá tiết điểm đích chỉ châm (Pointer)
Liên biểu đích cơ bổn nguyên tố:
- Tiết điểm: Mỗi cá tiết điểm hữu lưỡng cá bộ phân, tả biên bộ phân xưng vi trị vực, dụng lai tồn phóng dụng hộ sổ cư; hữu biên bộ phân xưng vi chỉ châm vực, dụng lai tồn phóng chỉ hướng hạ nhất cá nguyên tố đích chỉ châm.
- head:head tiết điểm vĩnh viễn chỉ hướng đệ nhất cá tiết điểm
- tail: tail vĩnh viễn chỉ hướng tối hậu nhất cá tiết điểm
- None: Liên biểu trung tối hậu nhất cá tiết điểm đích chỉ châm vực vi None trị
Thời gian phục tạp độ
Do vu bất tất tu án thuận tự tồn trữ, liên biểu tại sáp nhập đích thời hầu khả dĩ đạt đáo O(1) đích phục tạp độ, bỉ lánh nhất chủng tuyến tính biểu thuận tự biểu khoái đắc đa, đãn thị tra trảo nhất cá tiết điểm hoặc giả phóng vấn đặc định biên hào đích tiết điểm tắc nhu yếu O(n) đích thời gian, nhi thuận tự biểu tương ứng đích thời gian phục tạp độ phân biệt thị O(logn) hòa O(1).
Tiết điểm & liên biểu định nghĩa
Tiết điểm định nghĩa
#encoding=utf-8
class Node:
def __init__(self,value = None, next = None):
self.value = value
self.next = next
def __str__(self):
# trắc thí cơ bổn công năng, thâu xuất tự phù xuyến
return str(self.value)
print (Node( "text" ))
Liên biểu định nghĩa
Trục cá định nghĩa tiết điểm, tái bả mỗi cá tiết điểm đích quan hệ biểu kỳ xuất lai.
#encoding=utf-8
class Node:
def __init__(self,value = None, next = None):
self.value = value
self.next = next
def __str__(self):
return str(self.value)
# định nghĩa mỗi cá tiết điểm
node1=Node(1)
node2=Node(2)
node3=Node(3)
# định nghĩa quan hệ
node1.next=node2
node2.next=node3
Thuận tự đả ấn hòa nghịch tự đả ấn
Thuận tự đả ấn
Thông quá thâu nhập đệ nhất cá tiết điểm, tuần hoàn chỉnh cá liên biểu, thuận tự đả ấn chỉnh cá liên biểu
#encoding=utf-8
class Node:
def __init__(self,value = None, next = None):
self.value = value
self.next = next
def __str__(self):
return str(self.value)
# định nghĩa mỗi cá tiết điểm
node1=Node(1)
node2=Node(2)
node3=Node(3)
# định nghĩa quan hệ
node1.next=node2
node2.next=node3
def printlist(node):
while node:
print(node)
node=node.next
printlist(node1)
Vận hành kết quả
1
2
3
Nghịch tự đả ấn
Sử dụng đệ quy đích phương pháp đả ấn toàn bộ liên biểu nguyên tố, thuận tự vi nghịch tự
#encoding=utf-8
class Node:
def __init__(self,value = None, next = None):
self.value = value
self.next = next
def __str__(self):
return str(self.value)
# định nghĩa mỗi cá tiết điểm
node1=Node(1)
node2=Node(2)
node3=Node(3)
# định nghĩa quan hệ
node1.next=node2
node2.next=node3
def printBackward(node):
if node == None:
return
head = node
tail= node.next
printBackward(tail)
print (head,tail)
printBackward(node1)
Vận hành kết quả
3 None
2 3
1 2
Kế toán liên biểu đích trường độ
#encoding=utf-8
class Node(object):# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return self.data
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
print(len(LinkedList(node1)))
Vận hành kết quả
3
Tại liên biểu trung sáp nhập sổ cư
Tòng liên biểu đích tiền diện sáp nhập sổ cư
Bộ sậu:
- Bị sáp nhập sổ cư vi không, phản hồi
- Sử dụng cai thâu nhập sổ cư sang kiến nhất cá tiết điểm, tịnh tương cai tiết điểm đích next chỉ hướng nguyên lai đầu tiết điểm
- Thiết trí cai tiết điểm vi đầu tiết điểm
Đại mã thật hiện:
#encoding=utf-8
class Node(object):
# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return self.data
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
def insertToFront(self, data):
# công năng: Thâu nhập data, sáp nhập đáo đầu tiết điểm tiền, tịnh canh cải vi đầu tiết điểm
# thâu xuất: Đương tiền đầu tiết điểm
if data is None:
return None
node = Node(data, self.head)
self.head = node
return node
node4 = Node(4)
link_list = LinkedList(node1)# sang kiến nhất cá liên biểu thật lệ, liên biểu tòng node1 khai thủy
link_list.insertToFront(node4)
print(len(link_list))
Vận hành kết quả
4
Tòng liên biểu đích hậu diện sáp nhập sổ cư
Bộ sậu
- Nhược thâu nhập sổ cư vi không, phản hồi None
- Nhược đầu tiết điểm vi không, trực tiếp tương thâu nhập sổ cư tác vi đầu tiết điểm
- Nhược phi không, tắc biến lịch chỉnh cá liên biểu, trực đáo đương tiền tiết điểm đích hạ nhất cá tiết điểm vi None thời, tương đương tiền tiết điểm đích hạ nhất cá tiết điểm thiết trí vi thâu nhập sổ cư
Đại mã thật hiện:
#encoding=utf-8
class Node(object):
# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return self.data
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
def insertToFront(self, data):
# công năng: Thâu nhập data, sáp nhập đáo đầu tiết điểm tiền, tịnh canh cải vi đầu tiết điểm
# thâu xuất: Đương tiền đầu tiết điểm
if data is None:
return None
node = Node(data, self.head)
self.head = node
return node
def append(self, data):
# công năng: Thâu nhập data, tác vi tiết điểm sáp nhập đáo mạt vĩ
if data is None:
return None
node = Node(data)
if self.head is None:
self.head = node
return node
curr_node = self.head
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node
node4 = Node(4)
link_list = LinkedList(node1)
link_list.append(node4)
print(len(link_list))
Tại liên biểu đích trung gian sáp nhập sổ cư
#encoding=utf-8
class Node(object):
# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
def insertToFront(self, data):
# công năng: Thâu nhập data, sáp nhập đáo đầu tiết điểm tiền, tịnh canh cải vi đầu tiết điểm
# thâu xuất: Đương tiền đầu tiết điểm
if data is None:
return None
node = Node(data, self.head)
self.head = node
return node
def append(self, data):
# công năng: Thâu nhập data, tác vi tiết điểm sáp nhập đáo mạt vĩ
if data is None:
return None
node = Node(data)
if self.head is None:
self.head = node
return node
curr_node = self.head
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node
def insertAmong(self,data,site):
# công năng: Thâu nhập data, sáp đáo chỉ định site tiền.
# thâu xuất: Sáp nhập đích data
if data is None:
return None
if self.head is None:
return None
if site is None:
return None
node = Node(data)
curr_node = self.head
while curr_node.next is not None:
if curr_node.next.data==site:
tmp=curr_node.next
curr_node.next=node
node.next=tmp
return node
else:
curr_node=curr_node.next
return 'insert fail'
link_list = LinkedList(node1)
print(link_list.insertAmong(5,2))
print(len(link_list )
Tại liên biểu trung tra trảo nguyên tố
#encoding=utf-8
class Node(object):
# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
def insertToFront(self, data):
# công năng: Thâu nhập data, sáp nhập đáo đầu tiết điểm tiền, tịnh canh cải vi đầu tiết điểm
# thâu xuất: Đương tiền đầu tiết điểm
if data is None:
return None
node = Node(data, self.head)
self.head = node
return node
def append(self, data):
# công năng: Thâu nhập data, tác vi tiết điểm sáp nhập đáo mạt vĩ
if data is None:
return None
node = Node(data)
if self.head is None:
self.head = node
return node
curr_node = self.head
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node
def find(self, data):
# công năng: Tra trảo liên biểu đích tiết điểm data dữ data tương đồng đích tiết điểm
if data is None:
return None
curr_node = self.head
while curr_node is not None:
if curr_node.data == data:
return curr_node
curr_node = curr_node.next
return None
link_list = LinkedList(node1)
print(link_list.find(2))
San trừ nguyên tố
Chỉ định nghĩa nhất cá biến lượng tác vi đương tiền tiết điểm, sử dụng tha đích hạ nhất cá tiết điểm khứ phán đoạn thị phủ dữ sổ cư sổ cư thất phối, nhược thất phối, trực tiếp tương đương tiền tiết điểm chỉ hướng hạ hạ nhất cá tiết điểm.
#encoding=utf-8
class Node(object):
# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
def insertToFront(self, data):
# công năng: Thâu nhập data, sáp nhập đáo đầu tiết điểm tiền, tịnh canh cải vi đầu tiết điểm
# thâu xuất: Đương tiền đầu tiết điểm
if data is None:
return None
node = Node(data, self.head)
self.head = node
return node
def append(self, data):
# công năng: Thâu nhập data, tác vi tiết điểm sáp nhập đáo mạt vĩ
if data is None:
return None
node = Node(data)
if self.head is None:
self.head = node
return node
curr_node = self.head
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node
def find(self, data):
# công năng: Tra trảo liên biểu đích tiết điểm data dữ data tương đồng đích tiết điểm
if data is None:
return None
curr_node = self.head
while curr_node is not None:
if curr_node.data == data:
return curr_node
curr_node = curr_node.next
return None
def deleteData(self,data):
# chỉ định nghĩa nhất cá biến lượng lai hoàn thành san trừ thao tác
if data is None:
return
if self.head is None:
return
if self.head.data == data:
self.head = self.head.next
return
curr_node = self.head
while curr_node.next is not None:
if curr_node.next.data == data:
curr_node.next = curr_node.next.next
return
curr_node = curr_node.next
link_list = LinkedList(node1)
print(link_list.deleteData(2))
print(len(link_list ))
PS: Bao hàm toàn bộ công năng đích đại mã
#encoding=utf-8
class Node(object):
# tiết điểm loại
# công năng: Thâu nhập nhất cá trị data, tương trị biến vi nhất cá tiết điểm
def __init__(self, data, next = None):
self.data = data
self.next = next
def __str__(self):
return str(self.data)
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node1.next = node2
node2.next = node3
class LinkedList(object):
def __init__(self, head = None):
self.head = head
def __len__(self):
# công năng: Thâu nhập đầu tiết điểm, phản hồi liên biểu trường độ
curr = self.head
counter = 0
while curr is not None:
counter += 1
curr = curr.next
return counter
def insertToFront(self, data):
# công năng: Thâu nhập data, sáp nhập đáo đầu tiết điểm tiền, tịnh canh cải vi đầu tiết điểm
# thâu xuất: Đương tiền đầu tiết điểm
if data is None:
return None
node = Node(data, self.head)
self.head = node
return node
def append(self, data):
# công năng: Thâu nhập data, tác vi tiết điểm sáp nhập đáo mạt vĩ
if data is None:
return None
node = Node(data)
if self.head is None:
self.head = node
return node
curr_node = self.head
while curr_node.next is not None:
curr_node = curr_node.next
curr_node.next = node
return node
def insertAmong(self,data,site):
# công năng: Thâu nhập data, sáp đáo chỉ định site tiền.
# thâu xuất: Sáp nhập đích data
if data is None:
return None
if self.head is None:
return None
if site is None:
return None
node = Node(data)
curr_node = self.head
while curr_node.next is not None:
if curr_node.next.data==site:
tmp=curr_node.next
curr_node.next=node
node.next=tmp
return node
else:
curr_node=curr_node.next
return 'insert fail'
def printList(self):
# công năng: Thuận tự đả ấn liên biểu. Tương liên biểu thuận tự trữ tồn tại nhất cá list trung.
l=[]
curr = self.head
while curr:
l.append(curr.data)
curr=curr.next
return l
def find(self, data):
# công năng: Tra trảo liên biểu đích tiết điểm data dữ data tương đồng đích tiết điểm
if data is None:
return None
curr_node = self.head
while curr_node is not None:
if curr_node.data == data:
return curr_node
curr_node = curr_node.next
return None
def deleteData(self,data):
# chỉ định nghĩa nhất cá biến lượng lai hoàn thành san trừ thao tác
if data is None:
return
if self.head is None:
return
if self.head.data == data:
self.head = self.head.next
return
curr_node = self.head
while curr_node.next is not None:
if curr_node.next.data == data:
curr_node.next = curr_node.next.next
return
curr_node = curr_node.next
link_list = LinkedList(node1)
print(link_list.insertAmong(5,2))
print(len(link_list ))
print(link_list.printList())