卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

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

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类来实现一个链表,以及如何向链表中添加元素。我们重点介绍了在列表的开头和结尾添加元素的方法。

卓越飞翔博客
上一篇: 在PHP中的ftp_put()函数
下一篇: 如何在C#中将整数转换为带有零填充的字符串?
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏