目錄
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 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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡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脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
3 週前 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)

熱門話題

Java教學
1665
14
CakePHP 教程
1424
52
Laravel 教程
1321
25
PHP教程
1269
29
C# 教程
1249
24
使用遞歸方法在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

Go 語言中的鍊錶操作怎麼實作? Go 語言中的鍊錶操作怎麼實作? Jun 10, 2023 pm 10:55 PM

鍊錶(LinkedList)是一種常見的資料結構,它由一系列結點(Node)組成,每一個結點包含兩個關鍵屬性:資料域(Data)和指標域(Next)。其中,數據域用於儲存實際數據,而指標域則指向下一個結點。透過這種方式,鍊錶以一種靈活的方式儲存數據,適用於許多不同的應用場景。在Go語言中,鍊錶結構也得到了良好的支援。 Go的內建標準庫中提供了cont

See all articles