在Python中使用 dstructure 库的链表
Dstructure 是一个Python库,用于创建链表、堆栈、队列、哈希图、树等数据结构。链表是一种线性数据结构,其中元素不存储在连续的内存位置。链表中的元素使用指针链接,如下图所示:
pip install dstructure
让我们看看如何使用这个库创建不同类型的链表。
1. 单链表
单向链表包含具有数据字段和“下一个”字段的节点,该字段指向节点行中的下一个节点。可以对单向链表执行的操作包括插入、删除和遍历。
Python3
from dstructure.ll.SLL import SLL
obj = SLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.insert(50) # insert 50 in linked list
obj.insert(60) # insert 60 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 -> 30 -> 40
Python3
from dstructure.ll.DLL import DLL
obj = DLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 <-> 30 <-> 40
Python3
from dstructure.ll.SCLL import SCLL
obj = SCLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 -> 30 -> 40
Python3
from dstructure.ll.DCLL import DCLL
obj = DCLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 <-> 30 <-> 40
输出:
[30,40,50]
30 -> 40 -> 50
2.双向链表。
双向链表是一种链表,其中每个节点除了存储其数据外,还有两个链接。第一个链接指向列表中的前一个节点,第二个链接指向列表中的下一个节点。
蟒蛇3
from dstructure.ll.DLL import DLL
obj = DLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 <-> 30 <-> 40
输出:
10 <-> 30 <-> 40
3.单循环链表。
在单向链表中,为了访问链表的任何节点,我们从第一个节点开始遍历。如果我们位于列表中间的任何节点,则无法访问给定节点之前的节点。这个问题可以通过稍微改变单链表的结构来解决。在单向链表中,下一部分(指向下一个节点的指针)为 NULL,如果我们利用此链接指向第一个节点,那么我们可以到达前面的节点。
蟒蛇3
from dstructure.ll.SCLL import SCLL
obj = SCLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 -> 30 -> 40
输出:
10 -> 30 -> 40
4.双循环链表。
循环双向链表具有双向链表和循环链表的性质,其中两个连续元素通过前后指针链接或连接,最后一个节点通过下一个指针指向第一个节点,第一个节点通过下一个指针指向最后一个节点前一个指针。
蟒蛇3
from dstructure.ll.DCLL import DCLL
obj = DCLL()
obj.insert(10) # insert 10 in linked list
obj.insert(20) # insert 20 in linked list
obj.insert(30) # insert 30 in linked list
obj.insert(40) # insert 40 in linked list
obj.delete_f() # delete first node in linked list
obj.delete_l() # delete last node in linked list
obj.delete(20) # delete the node which we pass and return True otherwise False
obj.getnodes() # return all the node in linked list in list.
obj.print() # print all the in this format 10 <-> 30 <-> 40
输出:
10 <-> 30 <-> 40