0

0

Python程序:在链表的第一个和最后一个位置添加元素

王林

王林

发布时间:2023-08-23 23:17:04

|

2626人浏览过

|

来源于tutorialspoint

转载

python程序:在链表的第一个和最后一个位置添加元素

在Python中,链表是一种线性数据结构,它由一系列节点组成,每个节点包含一个值和对链表中下一个节点的引用。

在本文中,我们将讨论如何在Python中将元素添加到链表的第一个和最后一个位置。

Linked List in Python

链表是一种引用数据结构,用于存储一组元素。它在某种程度上类似于数组,但是在数组中,数据存储在连续的内存位置中,而在链表中,数据不受此条件限制。这意味着数据不是按顺序存储,而是以随机的方式存储在内存中。

This raises one question that is, how we can access the elements in a linked list? The answer is quite intuitive in linked list one element points to another till the end of the list.

立即学习Python免费学习笔记(深入)”;

列表的开头和结尾被视为特殊位置。列表的开头称为头部,它指向第一个元素,而最后一个元素则特殊之处在于它指向NULL

Head -> data_1 -> data_2 -> … -> data_n -> NULL

现在我们知道如何访问链表的开头和结尾,让我们看看如何遍历元素并访问链表中的数据。

遍历链表非常简单,我们只需从头开始访问下一个节点;我们不断重复这个过程,直到找到一个下一个节点为NULL的节点。至于访问节点中的数据,我们使用箭头运算符“->”。

Head->data

现在我们已经具备了所有必要的理解来开始解决这个问题。

在开头添加元素

To add the data at the beginning of the linked list, we must take into consideration the head of the linked list. Whenever we add a node at the beginning of the linked list, the linked list will be modified with the newly added node being the first node / head of the list.

Algorithm

Step 1 – Create the new node

步骤 2 - 在新创建的节点中添加数据

Step 3 – Update the link of the new node and make it point to current head node

漂亮的电子企业网站1.2
漂亮的电子企业网站1.2

这是一个免费的企业网站系统,任何人可以免费下载、修改和使用本程序,也可以用来为企业建网站。没有任何功能限制,且不发布收费版。容兴免费企业网站系统后台功能简介:1.基本设置:基本信息,联系方式,网站设置,导航管理,模块启闭,静态设置,安全设置,数据库管理2.产品管理:产品列表,添加产品,产品分类3.文章管理:文章列表,发表文章,文章分类,公司简介,网站公告4.客服互动:留言管理,在线客服,友情链接5

下载

步骤 4 - 现在将头指针设置为新创建的节点

注意 - 这些步骤的顺序非常重要,因为如果您首先将新创建的节点设置为头节点,那么我们将无法更新新节点的链接,理想情况下,该链接应该指向先前的头节点。

Example

class Node:
   def __init__(self, data):
      self.dataPart = data
      self.nextNode = None
class LinkedList:
   def __init__(self):
      self.headNode = None
   def showList(self):
      n = self.headNode
      while n is not None:
         print(n.dataPart, end='-')
         n = n.nextNode
      print('')
   def addBeginList(self, data):
      tempNode = Node(data)
      tempNode.nextNode = self.headNode
      self.headNode = tempNode
newLinkedList = LinkedList()
print("Printing the list before adding element : ")
newLinkedList.showList()
newLinkedList.addBeginList(10)
newLinkedList.addBeginList(25)
print("Printing the elements after adding at the beginning of the list")
newLinkedList.showList()

输出

Printing the list before adding any element :
\
Printing the elements after adding at the beginning of the list
25-10-\

在末尾添加元素

Adding elements at the end, is logically different from adding at the beginning of the list. This time we need to access the last node of the list instead of the first node, i.e., head.

现在的问题是要检查我们要添加元素的列表是否为空列表,或者它是否已经有一些元素。

If the list is empty then the new node will be the first node for the list, and in the other case, it will be the last node. For that we need to check whether the head node is None or not. The list is treated empty of head is None, and not empty otherwise.

Algorithm

Step 1 – Create a new node.

第二步 - 将数据添加到节点的数据部分。

Step 3 – Make sure the next node of the newly created node points to None or Null pointer.

步骤 4 - 如果列表为空,则将新创建的节点作为头节点。

Step 5 - Else traverse to the end of list, last node.

Step 6 – Set the next node of the last node to the newly created node.

Example

class Node:
   def __init__(self, data):
      self.dataPart = data
      self.nextNode = None
class LinkedList:
   def __init__(self):
      self.headNode = None
   def showList(self):
      n = self.headNode
      while n is not None:
         print(n.dataPart, end='-')
         n = n.nextNode
      print("")
   def addEndList(self, data):
      tempNode = Node(data)
      if self.headNode is None:
         self.headNode = tempNode
      else:
         n = self.headNode
         while n.nextNode is not None:
            n = n.nextNode
            n.nextNode = tempNode
newLinkedList = LinkedList()
print("Printing the list before insertion : ")
newLinkedList.showList()
newLinkedList.addEndList(25)
newLinkedList.addEndList(10)
print("Printing the list after adding elements at the end of the list : ")
newLinkedList.showList()

输出

Printing the list before insertion :
\
Printing the list after adding elements at the end of the list :
25-10-\

Conclusion

在本文中,我们讨论了如何使用Python类来实现一个链表,以及如何向链表中添加元素。我们重点介绍了在列表的开头和结尾添加元素的方法。

相关文章

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
excel制作动态图表教程
excel制作动态图表教程

本专题整合了excel制作动态图表相关教程,阅读专题下面的文章了解更多详细教程。

20

2025.12.29

freeok看剧入口合集
freeok看剧入口合集

本专题整合了freeok看剧入口网址,阅读下面的文章了解更多网址。

65

2025.12.29

俄罗斯搜索引擎Yandex最新官方入口网址
俄罗斯搜索引擎Yandex最新官方入口网址

Yandex官方入口网址是https://yandex.com;用户可通过网页端直连或移动端浏览器直接访问,无需登录即可使用搜索、图片、新闻、地图等全部基础功能,并支持多语种检索与静态资源精准筛选。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

197

2025.12.29

python中def的用法大全
python中def的用法大全

def关键字用于在Python中定义函数。其基本语法包括函数名、参数列表、文档字符串和返回值。使用def可以定义无参数、单参数、多参数、默认参数和可变参数的函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

python改成中文版教程大全
python改成中文版教程大全

Python界面可通过以下方法改为中文版:修改系统语言环境:更改系统语言为“中文(简体)”。使用 IDE 修改:在 PyCharm 等 IDE 中更改语言设置为“中文”。使用 IDLE 修改:在 IDLE 中修改语言为“Chinese”。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

16

2025.12.29

C++的Top K问题怎么解决
C++的Top K问题怎么解决

TopK问题可通过优先队列、partial_sort和nth_element解决:优先队列维护大小为K的堆,适合流式数据;partial_sort对前K个元素排序,适用于需有序结果且K较小的场景;nth_element基于快速选择,平均时间复杂度O(n),效率最高但不保证前K内部有序。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

12

2025.12.29

php8.4实现接口限流的教程
php8.4实现接口限流的教程

PHP8.4本身不内置限流功能,需借助Redis(令牌桶)或Swoole(漏桶)实现;文件锁因I/O瓶颈、无跨机共享、秒级精度等缺陷不适用高并发场景。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

134

2025.12.29

抖音网页版入口在哪(最新版)
抖音网页版入口在哪(最新版)

抖音网页版可通过官网https://www.douyin.com进入,打开浏览器输入网址后,可选择扫码或账号登录,登录后同步移动端数据,未登录仅可浏览部分推荐内容。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

63

2025.12.29

快手直播回放在哪看教程
快手直播回放在哪看教程

快手直播回放需主播开启功能才可观看,主要通过三种路径查看:一是从“我”主页进入“关注”标签再进主播主页的“直播”分类;二是通过“历史记录”中的“直播”标签页找回;三是进入“个人信息查阅与下载”里的“直播回放”选项。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

18

2025.12.29

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
CSS3 教程
CSS3 教程

共18课时 | 4.1万人学习

Excel 教程
Excel 教程

共162课时 | 10.1万人学习

SciPy 教程
SciPy 教程

共10课时 | 1.0万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号