Python 向链表的第一个和最后一个位置添加元素

Python 向链表的第一个和最后一个位置添加元素

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

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

Python中的链表

链表是一种引用型数据结构,它保存一组元素。它在数组方面与之相似,但是在数组中数据存储在连续的内存位置中,而在链表中,数据不受此条件限制。这意味着数据不是按照顺序存储,而是以随机的方式存储在内存中。

这就提出了一个问题,即我们如何访问链表中的元素?答案在链表中非常直观,一个元素指向另一个元素,直到链表的末尾。

列表的开头和末尾被认为是特殊的位置。列表的开头被称为头部,它指向第一个元素,而最后一个元素在某种意义上是特殊的,因为它指向NULL。

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

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

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

Head->data

现在我们已经具备了解决这个问题的所有必要知识。

在开头添加元素

要在链表的开头添加数据,我们必须考虑链表的头部。每当我们在链表的开头添加一个节点时,链表会被修改,新添加的节点将成为链表的第一个节点/头节点。

步骤

步骤 1 - 创建新节点

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

步骤 3 - 更新新节点的链接,使其指向当前的头节点

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

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

示例

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-\

在末尾添加元素

在末尾添加元素与在列表开头添加元素在逻辑上是不同的。这一次,我们需要访问列表的最后一个节点,而不是第一个节点,即头节点。

现在的问题是要检查我们要添加元素的列表是一个空列表还是已经有一些元素在其中。

如果列表是空的,那么新节点将成为列表的第一个节点,否则将成为最后一个节点。为此,我们需要检查头节点是否为None。如果头节点为None,表示列表为空;否则表示列表不为空。

步骤

步骤1 - 创建一个新的节点。

步骤2 - 将数据添加到新节点的数据部分。

步骤3 - 确保新创建节点的下一个节点指向None或空指针。

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

步骤5 - 否则,遍历到列表的末尾,即最后一个节点。

步骤6 - 将最后一个节点的下一个节点设置为新创建的节点。

示例

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-\

结论

在这篇文章中,我们讨论了如何使用Python类来实现一个链表,并且如何向链表中添加元素。我们着重讨论了如何在链表的开头和结尾添加元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程