热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

链表Python库

链表Python库原文:https://www.gees

链表 Python 库

原文:https://www . geesforgeks . org/python-library-for-link-list/

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


  1. 单链表:在这种情况下,节点指向紧接其后的节点

  2. 双向链表:在这种情况下,节点不仅引用它旁边的节点,还引用它前面的节点。


Python 中的链表:

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

方法一:使用 得克() 包裹。
这是 Python 中的一个内置类,显然是用于出列的,但是可以通过某种方式实现,使得它在特定条件下像链表一样工作。

下面是链表的实现:

Python 3

# 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)

输出:

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

下面是链表的实现:

Python 3

# 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()
# poping 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

方法 3: 使用结构链接

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

结构链接。LinkedList 和 structlinks。DoublyLikedList 模块可以用来制作链表。所有可以用列表执行的操作也可以用 structlinks 执行。LinkedList 类。

碰杯去文档

在命令行中键入命令:

pip install structlinks

下面是链表的一些方法的实现:

Python 3

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 appedning and inserting: {lst}')
print()
print('Pop and Remove')
# Pop and Remove
element = lst.pop(0)
lst.remove(10.0)
print(f'list after poping 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 appedning and inserting: [10 -> 0.0 -> 10 -> string -> another string]
Pop and Remove:
list after poping and removing: [0.0 -> string -> another string]

推荐阅读
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 本文介绍如何使用 Python 将一个字符串按照指定的行和元素分隔符进行两次拆分,最终将字符串转换为矩阵形式。通过两种不同的方法实现这一功能:一种是使用循环与 split() 方法,另一种是利用列表推导式。 ... [详细]
  • Python 异步编程:深入理解 asyncio 库(上)
    本文介绍了 Python 3.4 版本引入的标准库 asyncio,该库为异步 IO 提供了强大的支持。我们将探讨为什么需要 asyncio,以及它如何简化并发编程的复杂性,并详细介绍其核心概念和使用方法。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 本文探讨了 Objective-C 中的一些重要语法特性,包括 goto 语句、块(block)的使用、访问修饰符以及属性管理等。通过实例代码和详细解释,帮助开发者更好地理解和应用这些特性。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文详细介绍了 Apache Jena 库中的 Txn.executeWrite 方法,通过多个实际代码示例展示了其在不同场景下的应用,帮助开发者更好地理解和使用该方法。 ... [详细]
author-avatar
mobiledu2502887683
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有