首頁  >  文章  >  後端開發  >  Python程式:在鍊錶的第一個和最後一個位置加入元素

Python程式:在鍊錶的第一個和最後一個位置加入元素

王林
王林轉載
2023-08-23 23:17:041945瀏覽

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 ified with the newlymod 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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除