📜  链表的Python库

📅  最后修改于: 2022-05-13 01:54:47.745000             🧑  作者: Mango

链表的Python库

链表是编程中一种简单的数据结构,显然是用来存储数据并相应地检索数据。为了更容易想象,它更像是一个动态数组,其中数据元素通过指针链接(即当前记录指向它的下一条记录,下一条指向它之后的记录,这种情况一直持续到结构的末端),而不是紧密包装。
链表有两种类型:

  1. 单链表:在这种情况下,节点指向紧随其后的节点
  2. 双链表:在这种情况下,节点不仅引用它旁边的节点,还引用它之前的节点。

Python中的链表:

从Python开始,它没有像经典编程语言那样内置链表库。 Python确实有一个作为动态数组工作的内置类型列表,但它的操作不应与链表的典型函数相混淆。这并不意味着不能在Python中实现链表,他们可以,但不会是直截了当的。以下方法可用于在Python中实现链表,因为它没有内置库:

方法一:使用deque()包。
这是Python中的一个内置类,显然用于出队,但可以以在某些条件下像链表一样工作的方式实现。

下面是链表的实现:

Python3
# importing module
import collections
 
# initialising a deque() of arbitrary length
linked_lst = collections.deque()
 
# filling deque() with elements
linked_lst.append('first')
linked_lst.append('second')
linked_lst.append('third')
 
print("elements in the linked_list:")
print(linked_lst)
 
# adding element at an arbitrary position
linked_lst.insert(1, 'fourth')
 
print("elements in the linked_list:")
print(linked_lst)
 
# deleting the last element
linked_lst.pop()
 
print("elements in the linked_list:")
print(linked_lst)
 
# removing a specific element
linked_lst.remove('fourth')
 
print("elements in the linked_list:")
print(linked_lst)


Python3
# importing packages
import llist
from llist import sllist,sllistnode
 
# creating a singly linked list
lst = sllist(['first','second','third'])
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# adding and inserting values
lst.append('fourth')
node = lst.nodeat(2)
 
lst.insertafter('fifth',node)
 
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# popping a value
#i.e. removing the last entry
# of the list
lst.pop()
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# removing a specific element
node = lst.nodeat(1)
lst.remove(node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()


Python3
import structlinks
from structlinks import LinkedList
 
# create an empty linked list
lst = LinkedList()
 
# create a linked list with initial values
lst = LinkedList([1, 10.0, 'string'])
 
print(lst)
 
print()
 
print('Elements of list:')
 
# elements of the list
element0 = lst[0]
element1 = lst[1]
element2 = lst[2]
 
print(f'first element : {element0}')
print(f'second element : {element1 }')
print(f'third element : {element2}')
 
print()
 
print('Length of list:')
 
# Length of the list
length = len(lst)
 
print(f'size of the list : {length}')
 
print()
 
print('Set item:')
 
# Set item
lst[0] = 10
 
print(f'list after setting lst[0] to 10 : {lst}')
 
print()
 
print('Append And Insert:')
 
# Append And Insert
lst.append('another string')
lst.insert(1, 0.0)
 
print(f'list after appending and inserting: {lst}')
 
print()
 
print('Pop and Remove')
 
# Pop and Remove
element = lst.pop(0)
lst.remove(10.0)
 
print(f'list after popping and removing : {lst}')
print(f'pop function also returns the element : {element}')
 
# This code is contributed by eeshannarula29


输出:

elements in the linked_list:
deque(['first', 'second', 'third'])
elements in the linked_list:
deque(['first', 'fourth', 'second', 'third'])
elements in the linked_list:
deque(['first', 'fourth', 'second'])
elements in the linked_list:
deque(['first', 'second'])

何时使用 deque() 作为链表?

  • 只需要分别在前后插入和删除元素。从中间插入和删除元素变得非常耗时。
  • 原地反转,因为Python现在允许在原地反转元素。
  • 存储优先于性能,并非所有元素都有自己的单独节点

方法二:使用llist包。

llist是 CPython 的扩展模块,提供基本的链表数据结构。类型在您的命令行中的以下命令中给出:

pip install llist

下面是链表的实现:

Python3

# importing packages
import llist
from llist import sllist,sllistnode
 
# creating a singly linked list
lst = sllist(['first','second','third'])
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# adding and inserting values
lst.append('fourth')
node = lst.nodeat(2)
 
lst.insertafter('fifth',node)
 
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# popping a value
#i.e. removing the last entry
# of the list
lst.pop()
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()
 
# removing a specific element
node = lst.nodeat(1)
lst.remove(node)
print(lst)
print(lst.first)
print(lst.last)
print(lst.size)
print()

输出:

sllist([first, second, third])
sllistnode(first)
sllistnode(third)
3

sllist([first, second, third, fifth, fourth])
sllistnode(first)
sllistnode(fourth)
5

sllist([first, second, third, fifth])
sllistnode(first)
sllistnode(fifth)
4

sllist([first, third, fifth])
sllistnode(first)
sllistnode(fifth)
3

方法三:使用StructLinks

StructLinks 用于轻松访问和可视化不同的数据结构,包括链表、双链表、二叉树、图、堆栈和队列。

structlinks.LinkedList 和 structlinks.DoublyLikedList 模块可用于制作链表。可以使用列表执行的所有操作也可以使用 structlinks.LinkedList 类执行。

点击进入文档

在命令行中键入命令:

pip install structlinks

下面是链表部分方法的实现:

Python3

import structlinks
from structlinks import LinkedList
 
# create an empty linked list
lst = LinkedList()
 
# create a linked list with initial values
lst = LinkedList([1, 10.0, 'string'])
 
print(lst)
 
print()
 
print('Elements of list:')
 
# elements of the list
element0 = lst[0]
element1 = lst[1]
element2 = lst[2]
 
print(f'first element : {element0}')
print(f'second element : {element1 }')
print(f'third element : {element2}')
 
print()
 
print('Length of list:')
 
# Length of the list
length = len(lst)
 
print(f'size of the list : {length}')
 
print()
 
print('Set item:')
 
# Set item
lst[0] = 10
 
print(f'list after setting lst[0] to 10 : {lst}')
 
print()
 
print('Append And Insert:')
 
# Append And Insert
lst.append('another string')
lst.insert(1, 0.0)
 
print(f'list after appending and inserting: {lst}')
 
print()
 
print('Pop and Remove')
 
# Pop and Remove
element = lst.pop(0)
lst.remove(10.0)
 
print(f'list after popping and removing : {lst}')
print(f'pop function also returns the element : {element}')
 
# This code is contributed by eeshannarula29

输出:

[1 -> 10.0 -> string]

Elements of list:
1
10.0
string

Length of list:
3

Set item:
list after setting lst[0] to 10 : [10 -> 10.0 -> string]

Append and Insert:
list after appending and inserting: [10 -> 0.0 -> 10 -> string -> another string]

Pop and Remove:
list after poping and removing: [0.0 -> string -> another string]