您好,登錄后才能下訂單哦!
Python是如何實現單鏈表和雙向鏈表的?相信很多新手小白還沒學會這個技能,通過這篇文章的總結,希望你能學會。如下資料是實現單鏈表和雙向鏈表的代碼。
單鏈表:
# -*- coding:utf-8 -*-
class Node(object):
"""節點"""
def __init__(self,elem):
self.elem = elem
self.next = None
class SingleLinkList(object):
"""單鏈表"""
#頭結點
def __init__(self,node = None):
self._head = node
def is_empty(self):
if self._head == None:
return True
else :
return False
def length(self):
cur = self._head
count = 0
while cur != None:
cur = cur.next
count += 1
return count
def travel(self):
"""遍歷整個鏈表"""
cur = self._head
if cur == None:
return
count = 0
while cur != None:
print(cur.elem,end=" ")
cur = cur.next
count += 1
return count
def add(self,item):
"""鏈表頭部添加元素"""
node =Node(item)
node.next = self._head
self._head = node
def append(self,item):
"""鏈表尾部添加元素,叫尾插法"""
node = Node(item)
if self.is_empty():
self._head = node
else:
cur = self._head
while cur.next !=None:
cur = cur.next
cur.next = node
def insert(self,pos,item):
"""指定位置添加元素
:param pos 從0開始
"""
if pos <= 0:
self.add(item)
elif pos >= self.length():
self.append(item)
else :
pre = self._head
count = 0
while count <= pos -1:
pre = pre.next
count += 1
node = Node(item)
node.next = pre.next
pre.next = node
def remove(self,item):
"""刪除指定的節點"""
cur = self._head
pre = None
while cur != None:
if cur.elem == item:
#判斷是否為頭結點
if cur == self._head:
self._head = cur.next
break
else :
pre.next = cur.next
break
else :
pre = cur
cur = cur.next
def search(self,item):
"""查找節點是否存在"""
cur = self._head
while cur != None:
if cur.elem == item:
return True
else :
cur = cur.next
return False
if __name__ == "__main__":
single_obj = SingleLinkList()
print(single_obj.is_empty()) #True
print(single_obj.length()) #0
single_obj.append(1)
print(single_obj.is_empty())
print(single_obj.length())
single_obj.append(2)
single_obj.add(8)
single_obj.append(3)
single_obj.append(4)
single_obj.append(5)
single_obj.append(6)
single_obj.travel() #8 1 2 3 4 5 6
print(" ")
single_obj.insert(-1,9)
single_obj.travel() #9 8 1 2 3 4 5 6
print(" ")
single_obj.insert(5,100)
single_obj.travel() #9 8 1 2 3 4 100 5 6
print(" ")
single_obj.insert(10,200)
single_obj.travel() #9 8 1 2 3 4 100 5 6 200
print(" ")
single_obj.remove(200)
single_obj.travel() # 9 8 1 2 3 4 100 5 6
雙向鏈表:
# -*- coding:utf-8 -*-
class Node(object):
"""節點"""
def __init__(self,item):
self.elem = item
self.prev = None
self.next = None
class Double_linked_list(object):
"""雙鏈表"""
def __init__(self,node = None):
self._head = node
def is_empty(self):
if self._head is None:
return True
else:
return False
def length(self):
cur = self._head
count = 0
while cur != None:
cur = cur.next
count += 1
return count
def travel(self):
"""遍歷整個鏈表"""
cur = self._head
if cur == None:
return
count = 0
while cur != None:
print(cur.elem,end=" ")
cur = cur.next
count += 1
return count
def add(self,item):
"""鏈表頭部添加元素"""
node =Node(item)
node.next = self._head
self._head = node
node.next.prev = node
def append(self,item):
"""鏈表尾部添加元素,叫尾插法"""
node = Node(item)
if self.is_empty():
self._head = node
else:
cur = self._head
while cur.next !=None:
cur = cur.next
cur.next = node
node.prev = cur
def insert(self,pos,item):
"""指定位置添加元素
:param pos 從0開始
"""
if pos <= 0:
self.add(item)
elif pos >= self.length():
self.append(item)
else :
cur = self._head
count = 0
while count < pos :
cur = cur.next
count += 1
node = Node(item)
node.next = cur
node.prev = cur.prev
cur.prev.next = node
cur.prev = node
def remove(self,item):
"""刪除指定的節點"""
cur = self._head
while cur != None:
if cur.elem == item:
#判斷是否為頭結點
if cur == self._head:
self._head = cur.next
if cur.next:
#判斷鏈表是否只有一個節點
cur.next.prev = None
break
else :
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else :
pre = cur
cur = cur.next
def search(self,item):
"""查找節點是否存在"""
cur = self._head
while cur != None:
if cur.elem == item:
return True
else :
cur = cur.next
return False
if __name__ == "__main__":
linkl = Double_linked_list()
print(linkl.is_empty())
print(linkl.length())
linkl.append(1)
print(linkl.is_empty())
print(linkl.length())
linkl.append(2)
linkl.add(8)
linkl.append(3)
linkl.append(4)
linkl.append(5)
linkl.append(6)
linkl.travel() #8 1 2 3 4 5 6
print(" ")
linkl.insert(-1,9)
linkl.travel() #9 8 1 2 3 4 5 6
print(" ")
linkl.insert(5,100)
linkl.travel() #9 8 1 2 3 4 100 5 6
print(" ")
linkl.insert(10,200)
linkl.travel() #9 8 1 2 3 4 100 5 6 200
print(" ")
linkl.remove(200)
linkl.travel() # 9 8 1 2 3 4 100 5 6
以上就是實現單鏈表和雙向鏈表的代碼,詳細使用情況還需要大家自己親自動手使用過才能領會。如果想了解更多相關內容,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。