目录
Linked List in Python
在开头添加元素
Algorithm
Example
输出
在末尾添加元素
Conclusion
首页 后端开发 Python教程 Python程序:在链表的第一个和最后一个位置添加元素

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

Aug 23, 2023 pm 11:17 PM
链表 添加元素

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.

列表的开头和结尾被视为特殊位置。列表的开头称为头部,它指向第一个元素,而最后一个元素则特殊之处在于它指向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

步骤 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程序:在链表的第一个和最后一个位置添加元素的详细内容。更多信息请关注PHP中文网其他相关文章!

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

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

使用递归方法在C++中找到链表倒数第n个节点 使用递归方法在C++中找到链表倒数第n个节点 Sep 15, 2023 pm 05:53 PM

给定一个单链表和正整数N作为输入。目标是使用递归找到给定列表中从末尾算起的第N个节点。如果输入列表有节点a→b→c→d→e→f并且N为4,那么倒数第4个节点将是c。我们将首先遍历直到列表中的最后一个节点以及从递归(回溯)增量计数返回时。当count等于N时,则返回指向当前节点的指针作为结果。让我们看看此的各种输入输出场景-输入-List:-1→5→7→12→2→96→33N=3输出−倒数第N个节点为:2解释−第三个节点是2。输入−列表:-12→53→8→19→20→96→33N=8输出-节点不存

Java程序向LinkedList添加元素 Java程序向LinkedList添加元素 Aug 26, 2023 pm 10:21 PM

LinkedList是JavaCollectionFramework的通用类,它实现了List、Deque和Queue三个接口。它提供了LinkedList数据结构的功能,LinkedList是一种线性数据结构,其中每个元素相互链接。我们可以对LinkedList执行多种操作,包括添加、删除和遍历元素。要将元素添加到LinkedList集合中,我们可以使用各种内置方法,例如add()、addFirst()和addLast()。我们将探索如何使用这些方法将元素添加到LinkedList。在Java

PHP SPL 数据结构:为你的项目注入速度和灵活性 PHP SPL 数据结构:为你的项目注入速度和灵活性 Feb 19, 2024 pm 11:00 PM

PHPSPL数据结构库概述PHPSPL(标准php库)数据结构库包含一组类和接口,用于存储和操作各种数据结构。这些数据结构包括数组、链表、栈、队列和集合,每个数据结构都提供了一组特定的方法和属性,用于操纵数据。数组在PHP中,数组是存储一系列元素的有序集合。SPL数组类提供了对原生的PHP数组进行加强的功能,包括排序、过滤和映射。以下是使用SPL数组类的一个示例:useSplArrayObject;$array=newArrayObject(["foo","bar","baz"]);$array

PHP 数组和链表的算法时间复杂度比较 PHP 数组和链表的算法时间复杂度比较 May 07, 2024 pm 01:54 PM

数组和链表的算法时间复杂度比较:访问数组O(1),链表O(n);插入数组O(1),链表O(1)/O(n);删除数组O(1),链表O(n);搜索数组O(n),链表O(n)。

将一个以链表表示的数字加1 将一个以链表表示的数字加1 Aug 29, 2023 pm 09:17 PM

数字的链表表示是这样提供的:链表的所有节点都被视为数字的一位数字。节点存储数字,使得链表的第一个元素保存数字的最高有效位,链表的最后一个元素保存数字的最低有效位。例如,数字202345在链表中表示为(2->0->2->3->4->5)。要向这个表示数字的链表添加1,我们必须检查列表中最低有效位的值。如果小于9就可以了,否则代码将更改下一个数字,依此类推。现在让我们看一个示例来了解如何做到这一点,1999表示为(1->9->9->9)并添加1应该将其

PHP数据结构:链表的魅力,探索动态数据组织 PHP数据结构:链表的魅力,探索动态数据组织 Jun 04, 2024 pm 12:53 PM

链表是一种数据结构,采用一系列带有数据和指针的节点组织元素,特别适合处理大型数据集和频繁的插入/删除操作。它的基本组成部分包括节点(数据和指向下一个节点的指针)和头节点(指向链表中第一个节点)。常见链表操作包括:添加(尾部插入)、删除(特定值)和遍历。

Python程序:在链表的第一个和最后一个位置添加元素 Python程序:在链表的第一个和最后一个位置添加元素 Aug 23, 2023 pm 11:17 PM

在Python中,链表是一种线性数据结构,它由一系列节点组成,每个节点包含一个值和对链表中下一个节点的引用。在本文中,我们将讨论如何在Python中将元素添加到链表的第一个和最后一个位置。LinkedListinPython链表是一种引用数据结构,用于存储一组元素。它在某种程度上类似于数组,但是在数组中,数据存储在连续的内存位置中,而在链表中,数据不受此条件限制。这意味着数据不是按顺序存储,而是以随机的方式存储在内存中。Thisraisesonequestionthatis,howwecanac

在Java中向数组添加元素时需要注意的技巧和事项 在Java中向数组添加元素时需要注意的技巧和事项 Jan 03, 2024 pm 02:01 PM

Java中数组添加元素的技巧和注意事项在Java中,数组是一种非常常见且重要的数据结构。它可以存储一组相同类型的元素,并且可以通过索引访问和修改这些元素。在实际应用中,我们经常需要向数组中动态地添加元素。本文将介绍一些Java中数组添加元素的技巧和注意事项,并提供相应的代码示例。使用动态数组(ArrayList)来添加元素动态数组ArrayList是

See all articles