您好,登錄后才能下訂單哦!
Python是如何實現(xiàn)單鏈表和雙向鏈表的?相信很多新手小白還沒學(xué)會這個技能,通過這篇文章的總結(jié),希望你能學(xué)會。如下資料是實現(xiàn)單鏈表和雙向鏈表的代碼。
單鏈表:
# -*- coding:utf-8 -*-
class Node(object):
"""節(jié)點"""
def __init__(self,elem):
self.elem = elem
self.next = None
class SingleLinkList(object):
"""單鏈表"""
#頭結(jié)點
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):
"""刪除指定的節(jié)點"""
cur = self._head
pre = None
while cur != None:
if cur.elem == item:
#判斷是否為頭結(jié)點
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):
"""查找節(jié)點是否存在"""
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):
"""節(jié)點"""
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):
"""刪除指定的節(jié)點"""
cur = self._head
while cur != None:
if cur.elem == item:
#判斷是否為頭結(jié)點
if cur == self._head:
self._head = cur.next
if cur.next:
#判斷鏈表是否只有一個節(jié)點
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):
"""查找節(jié)點是否存在"""
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
以上就是實現(xiàn)單鏈表和雙向鏈表的代碼,詳細使用情況還需要大家自己親自動手使用過才能領(lǐng)會。如果想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。