C# Linked List(對於開發者的運行原理)
鏈結列表是一種由一系列節點(也可稱為元素)組成的線性資料結構。 與元素/節點儲存於連續記憶體位置的陣列不同,連結式清單利用動態記憶體分配,允許元素/節點分散在整個記憶體中。
在最簡單的形式中,"連結清單"是由以線性方式連結在一起的節點所組成。 每個節點包含兩個主要部分:
1.數據:儲存在節點中的有效載荷。 這可以是任何資料類型,視實作而定,例如整數、字串、物件等。 2.下一個指標:指向序列中下一個節點的引用(或指標)。 此指針指示連結清單中下列節點向前的記憶體位置。
鏈結清單中的最後一個節點通常指向一個空引用,表示清單的結束。
在這篇文章中,我們將詳細介紹 C# 中的 Linked list,同時也會探討 IronPDF 函式庫,這是來自 Iron Software 的 PDF 產生工具。
連結式清單的類型
1.單一連結清單
單連結清單中的節點只有一個參照,通常指向序列中的下一個節點。 遍歷清單僅限於朝一個方向移動,通常從頭部 (初始節點) 到尾部 (最終節點)。
2.雙重鏈結列表
在雙重鏈結清單中,每個節點包含兩個參考:一個指向下一個節點,另一個指向序列中的上一個節點。 這種雙向連結可讓您在向前和向後兩個方向進行遍歷。
3.圓形連結清單
在迴圈鏈結清單中,最後一個節點會指向第一個節點,形成一個迴圈結構。 這種類型的連結清單可以使用單重或雙重連結節點來實作。
連結清單的基本操作
1.插入:在清單的特定位置(例如開頭、結尾或中間)新增一個節點到清單中。 2.刪除:從清單中移除指定的物件節點,並相應地調整相鄰節點的指標。 3.遍歷:遍歷清單以存取或操作每個節點的資料。 4.搜尋:根據資料指定的值在清單中尋找特定節點。
C# 中的鏈結清單
在 C# 中,您可以使用System.Collections.Generic 命名空間中的 LinkedList類別來實作鍊錶。 以下是所有基本操作的範例:
using System;
using System.Collections.Generic;
namespace CsharpSamples
{
public class Program
{
public static void Main()
{
// Create a new linked list of integers
LinkedList<int> linkedList = new LinkedList<int>();
// Add elements to the linked list
linkedList.AddLast(10);
linkedList.AddLast(20);
linkedList.AddLast(30);
linkedList.AddLast(40);
// Traverse and print the elements of the linked list
Console.WriteLine("Traverse Linked List elements:");
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
// Display number of linked list elements
Console.WriteLine($"Number of Linked List elements: {linkedList.Count}");
// Find/Search for an element in the linked list
Console.WriteLine("\nFind/Search Element Linked List elements: 30");
var foundNode = linkedList.Find(30);
if (foundNode != null)
{
Console.WriteLine(
$"Found Value: {foundNode.Value}, " +
$"Next Element: {(foundNode.Next != null ? foundNode.Next.Value.ToString() : "null")}, " +
$"Previous Element: {(foundNode.Previous != null ? foundNode.Previous.Value.ToString() : "null")}"
);
}
// Insert an element at a specified node
LinkedListNode<int> current = linkedList.Find(20);
if (current != null)
{
linkedList.AddAfter(current, 25);
}
Console.WriteLine($"\nNumber of Linked List elements: {linkedList.Count}");
Console.WriteLine("\nLinked List elements after insertion:");
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
// Remove an existing node from the linked list
linkedList.Remove(30);
Console.WriteLine("\nLinked List elements after removal:");
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
Console.WriteLine($"\nNumber of Linked List elements: {linkedList.Count}");
}
}
}
using System;
using System.Collections.Generic;
namespace CsharpSamples
{
public class Program
{
public static void Main()
{
// Create a new linked list of integers
LinkedList<int> linkedList = new LinkedList<int>();
// Add elements to the linked list
linkedList.AddLast(10);
linkedList.AddLast(20);
linkedList.AddLast(30);
linkedList.AddLast(40);
// Traverse and print the elements of the linked list
Console.WriteLine("Traverse Linked List elements:");
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
// Display number of linked list elements
Console.WriteLine($"Number of Linked List elements: {linkedList.Count}");
// Find/Search for an element in the linked list
Console.WriteLine("\nFind/Search Element Linked List elements: 30");
var foundNode = linkedList.Find(30);
if (foundNode != null)
{
Console.WriteLine(
$"Found Value: {foundNode.Value}, " +
$"Next Element: {(foundNode.Next != null ? foundNode.Next.Value.ToString() : "null")}, " +
$"Previous Element: {(foundNode.Previous != null ? foundNode.Previous.Value.ToString() : "null")}"
);
}
// Insert an element at a specified node
LinkedListNode<int> current = linkedList.Find(20);
if (current != null)
{
linkedList.AddAfter(current, 25);
}
Console.WriteLine($"\nNumber of Linked List elements: {linkedList.Count}");
Console.WriteLine("\nLinked List elements after insertion:");
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
// Remove an existing node from the linked list
linkedList.Remove(30);
Console.WriteLine("\nLinked List elements after removal:");
foreach (var item in linkedList)
{
Console.WriteLine(item);
}
Console.WriteLine($"\nNumber of Linked List elements: {linkedList.Count}");
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Namespace CsharpSamples
Public Class Program
Public Shared Sub Main()
' Create a new linked list of integers
Dim linkedList As New LinkedList(Of Integer)()
' Add elements to the linked list
linkedList.AddLast(10)
linkedList.AddLast(20)
linkedList.AddLast(30)
linkedList.AddLast(40)
' Traverse and print the elements of the linked list
Console.WriteLine("Traverse Linked List elements:")
For Each item In linkedList
Console.WriteLine(item)
Next item
' Display number of linked list elements
Console.WriteLine($"Number of Linked List elements: {linkedList.Count}")
' Find/Search for an element in the linked list
Console.WriteLine(vbLf & "Find/Search Element Linked List elements: 30")
Dim foundNode = linkedList.Find(30)
If foundNode IsNot Nothing Then
Console.WriteLine($"Found Value: {foundNode.Value}, " & $"Next Element: {(If(foundNode.Next IsNot Nothing, foundNode.Next.Value.ToString(), "null"))}, " & $"Previous Element: {(If(foundNode.Previous IsNot Nothing, foundNode.Previous.Value.ToString(), "null"))}")
End If
' Insert an element at a specified node
Dim current As LinkedListNode(Of Integer) = linkedList.Find(20)
If current IsNot Nothing Then
linkedList.AddAfter(current, 25)
End If
Console.WriteLine($vbLf & "Number of Linked List elements: {linkedList.Count}")
Console.WriteLine(vbLf & "Linked List elements after insertion:")
For Each item In linkedList
Console.WriteLine(item)
Next item
' Remove an existing node from the linked list
linkedList.Remove(30)
Console.WriteLine(vbLf & "Linked List elements after removal:")
For Each item In linkedList
Console.WriteLine(item)
Next item
Console.WriteLine($vbLf & "Number of Linked List elements: {linkedList.Count}")
End Sub
End Class
End Namespace
程式碼解釋
- 使用
new LinkedList<int>()建立新的整數鍊錶。 2.將指定值物件加入連結清單。 - 使用循環遍歷並列印鍊錶的元素。 4.尋找/搜尋連結清單中的元素。
- 使用
Find和AddAfter方法在指定節點插入元素。 - 使用
Remove方法從鍊錶中刪除現有節點。
輸出

介紹 IronPDF。
發現更多關於 IronPDF 的資訊 IronPDF 是由 Iron Software 開發和維護的功能強大的 C# PDF 函式庫。 它提供了一套全面的功能,可在 .NET 專案中建立、編輯 PDF 文件,並從 PDF 文件中萃取內容。
關於 IronPDF 的重點
HTML 至 PDF 轉換
IronPDF 可讓您將 HTML 內容轉換為 PDF 格式。 您可以輕鬆地將 HTML 頁面、URL 和 HTML 字串呈現為 PDF。
豐富的 API
這個函式庫提供使用者友善的 API,讓開發人員可以直接從 HTML 產生專業品質的 PDF。 無論您需要建立發票、報告或其他文件,IronPDF 都能簡化流程。
跨平台支援
IronPDF 與各種 .NET 環境相容,包括 .NET Core、.NET Standard 和 .NET Framework。 它可在 Windows、Linux 和 macOS 平台上執行。
通用性
IronPDF 支援不同的專案類型,例如網頁應用程式 (Blazor 和 WebForms)、桌面應用程式 (WPF 和 MAUI) 以及主控台應用程式。
內容來源
您可以從各種內容來源產生 PDF,包括 HTML 檔案、Razor 視圖 (Blazor Server)、CSHTML (MVC 和 Razor)、ASPX (WebForms) 和 XAML (MAUI)。
其他功能
1.為 PDF 加入頁首與頁尾。 2.合併、分割、新增、複製及刪除 PDF 頁面。 3.設定密碼、權限和數位簽章。 4.透過多執行緒和異步支援來最佳化效能。
相容性
IronPDF 遵循 PDF 標準,包括 1.2 至 1.7 版、PDF/UA 和 PDF/A。 它也支援 UTF-8 字元編碼、基本 URL 和資產編碼。
使用 LinkedList 產生 PDF 文檔
現在讓我們使用 IronPDF 建立一個 PDF 文檔,並示範 LinkedList 字串的用法。
首先,開啟 Visual Studio 並從專案範本中選擇建立一個主控台應用程式,如下所示。

提供專案名稱和地點。

選擇所需的 .NET 版本。

從 Visual Studio 套件管理員安裝 IronPDF,如下所示。

或使用以下指令列安裝。
dotnet add package IronPdf --version 2024.4.2
加入以下程式碼。
using System;
using System.Collections.Generic;
using IronPdf;
namespace CsharpSamples
{
public class Program
{
public static void Main()
{
var content = "<h1>Demonstrate IronPDF with C# LinkedList</h1>";
content += "<h2>Create a new linked list of strings</h2>";
content += "<p>Create a new linked list of strings with new LinkedList<string>()</p>";
// Create a new linked list of strings
LinkedList<string> linkedList = new LinkedList<string>();
// Add elements to the linked list
content += "<p>Add Apple to linkedList</p>";
linkedList.AddLast("Apple");
content += "<p>Add Banana to linkedList</p>";
linkedList.AddLast("Banana");
content += "<p>Add Orange to linkedList</p>";
linkedList.AddLast("Orange");
content += "<h2>Print the elements of the linked list</h2>";
Console.WriteLine("Linked List elements:");
foreach (var item in linkedList)
{
content += $"<p>{item}</p>";
Console.WriteLine(item);
}
content += "<h2>Insert an element at a specific position</h2>";
LinkedListNode<string> node = linkedList.Find("Banana");
if (node != null)
{
linkedList.AddAfter(node, "Mango");
content += "<p>Find Banana and insert Mango After</p>";
}
Console.WriteLine("\nLinked List elements after insertion:");
content += "<h2>Linked List elements after insertion:</h2>";
foreach (var item in linkedList)
{
content += $"<p>{item}</p>";
Console.WriteLine(item);
}
content += "<h2>Remove an element from the linked list</h2>";
linkedList.Remove("Orange");
content += "<p>Remove Orange from linked list</p>";
Console.WriteLine("\nLinked List elements after removal:");
content += "<h2>Linked List elements after removal:</h2>";
foreach (var item in linkedList)
{
content += $"<p>{item}</p>";
Console.WriteLine(item);
}
// Create a PDF renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from HTML string
var pdf = renderer.RenderHtmlAsPdf(content);
// Save to a file
pdf.SaveAs("AwesomeIronOutput.pdf");
}
}
}
using System;
using System.Collections.Generic;
using IronPdf;
namespace CsharpSamples
{
public class Program
{
public static void Main()
{
var content = "<h1>Demonstrate IronPDF with C# LinkedList</h1>";
content += "<h2>Create a new linked list of strings</h2>";
content += "<p>Create a new linked list of strings with new LinkedList<string>()</p>";
// Create a new linked list of strings
LinkedList<string> linkedList = new LinkedList<string>();
// Add elements to the linked list
content += "<p>Add Apple to linkedList</p>";
linkedList.AddLast("Apple");
content += "<p>Add Banana to linkedList</p>";
linkedList.AddLast("Banana");
content += "<p>Add Orange to linkedList</p>";
linkedList.AddLast("Orange");
content += "<h2>Print the elements of the linked list</h2>";
Console.WriteLine("Linked List elements:");
foreach (var item in linkedList)
{
content += $"<p>{item}</p>";
Console.WriteLine(item);
}
content += "<h2>Insert an element at a specific position</h2>";
LinkedListNode<string> node = linkedList.Find("Banana");
if (node != null)
{
linkedList.AddAfter(node, "Mango");
content += "<p>Find Banana and insert Mango After</p>";
}
Console.WriteLine("\nLinked List elements after insertion:");
content += "<h2>Linked List elements after insertion:</h2>";
foreach (var item in linkedList)
{
content += $"<p>{item}</p>";
Console.WriteLine(item);
}
content += "<h2>Remove an element from the linked list</h2>";
linkedList.Remove("Orange");
content += "<p>Remove Orange from linked list</p>";
Console.WriteLine("\nLinked List elements after removal:");
content += "<h2>Linked List elements after removal:</h2>";
foreach (var item in linkedList)
{
content += $"<p>{item}</p>";
Console.WriteLine(item);
}
// Create a PDF renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from HTML string
var pdf = renderer.RenderHtmlAsPdf(content);
// Save to a file
pdf.SaveAs("AwesomeIronOutput.pdf");
}
}
}
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports IronPdf
Namespace CsharpSamples
Public Class Program
Public Shared Sub Main()
Dim content = "<h1>Demonstrate IronPDF with C# LinkedList</h1>"
content &= "<h2>Create a new linked list of strings</h2>"
content &= "<p>Create a new linked list of strings with new LinkedList<string>()</p>"
' Create a new linked list of strings
Dim linkedList As New LinkedList(Of String)()
' Add elements to the linked list
content &= "<p>Add Apple to linkedList</p>"
linkedList.AddLast("Apple")
content &= "<p>Add Banana to linkedList</p>"
linkedList.AddLast("Banana")
content &= "<p>Add Orange to linkedList</p>"
linkedList.AddLast("Orange")
content &= "<h2>Print the elements of the linked list</h2>"
Console.WriteLine("Linked List elements:")
For Each item In linkedList
content &= $"<p>{item}</p>"
Console.WriteLine(item)
Next item
content &= "<h2>Insert an element at a specific position</h2>"
Dim node As LinkedListNode(Of String) = linkedList.Find("Banana")
If node IsNot Nothing Then
linkedList.AddAfter(node, "Mango")
content &= "<p>Find Banana and insert Mango After</p>"
End If
Console.WriteLine(vbLf & "Linked List elements after insertion:")
content &= "<h2>Linked List elements after insertion:</h2>"
For Each item In linkedList
content &= $"<p>{item}</p>"
Console.WriteLine(item)
Next item
content &= "<h2>Remove an element from the linked list</h2>"
linkedList.Remove("Orange")
content &= "<p>Remove Orange from linked list</p>"
Console.WriteLine(vbLf & "Linked List elements after removal:")
content &= "<h2>Linked List elements after removal:</h2>"
For Each item In linkedList
content &= $"<p>{item}</p>"
Console.WriteLine(item)
Next item
' Create a PDF renderer
Dim renderer = New ChromePdfRenderer()
' Create a PDF from HTML string
Dim pdf = renderer.RenderHtmlAsPdf(content)
' Save to a file
pdf.SaveAs("AwesomeIronOutput.pdf")
End Sub
End Class
End Namespace
程式碼解釋
1.首先,我們先使用內容字串物件建立 PDF 的內容。 內容會以 HTML 字串的形式產生。
- 建立一個新的字串鍊錶,其中包含
new LinkedList<string>()。 3.將元素新增至連結清單中,並將資訊附加至 PDF 內容字串。 4.列印連結清單的元素並附加到 PDF 內容。 - 使用
AddAfter方法在指定位置插入元素; 更新內容並列印出結果清單。 - 使用
Remove方法從鍊錶中刪除一個元素,更新內容,並列印結果清單。 - 最後,使用
ChromePdfRenderer、RenderHtmlAsPdf和SaveAs方法將產生的 HTML 內容字串儲存到 PDF 文件中。
輸出

輸出內容有水印,可使用 IronPDF授權頁面上的有效授權移除水印。
IronPDF 授權。
IronPDF函式庫需要授權才能執行,您可以從產品授權頁面取得授權。
將密鑰貼在下面的 appSettings.json 檔案中。
{
"IronPdf.License.LicenseKey": "The Key Goes Here"
}
結論
C# LinkedList 提供了一個多功能的資料結構來管理元素集合,提供高效的插入和刪除操作,同時適應動態調整大小,類似於預設的雜湊函數。 連結清單常用於各種應用程式和演算法,例如實作堆疊、佇列、符號表和記憶體管理系統。 瞭解連結清單的特性和操作對於建立有效率且可擴充的軟體解決方案是非常重要的。
總而言之,雖然連結清單在某些情況下表現優異,例如動態資料結構和頻繁插入/刪除,但對於需要頻繁隨機存取或處理記憶體受限環境的應用程式而言,連結清單可能不是最佳選擇。 仔細考慮資料的特定需求和特性,可以引導選擇最適合手邊工作的資料結構。
Iron Software 的 IronPDF 函式庫可讓開發人員毫不費力地建立和處理 PDF 文件,讓開發現代應用程式的進階技能得以實現。
常見問題解答
什麼是 C# 中的鏈結串列?
C# 中的鏈結串列是一種由節點組成的線性資料結構,每個節點包含資料和一個指向下一個節點的參考。與陣列不同,這種結構允許動態記憶體分配,使得元素可以儲存在非連續的記憶體位置。
怎樣在 C# 中將 HTML 轉換為 PDF?
您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF。它還允許您使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。
C# 中的鏈結串列類型有哪些?
在 C# 中,鏈結串列的主要類型有單鏈結串列、雙向鏈結串列和循環鏈結串列。單鏈結串列的節點只有一個指向下一個節點的參考,雙向鏈結串列有指向下一個和前一個節點的參考,循環鏈結串列的最後一個節點指向第一個節點。
可以對鏈結串列執行哪些基本操作?
鏈結串列支持的操作包括插入(添加新節點)、刪除(移除現有節點)、遍歷(迭代串列)以及搜尋(根據節點資料查找節點)。
如何在 C# 中實現鏈結串列?
可以使用來自 System.Collections.Generic 命名空間的 LinkedList 類別在 C# 中實現鏈結串列,該類別提供了用於添加、刪除和操作節點的方法。
PDF 生成庫提供了哪些功能?
像 IronPDF 這樣的 PDF 生成庫提供 HTML 到 PDF 的轉換、文字提取、文件合併和拆分、設置文件權限等功能,涵蓋了各種 .NET 環境。
如何使用鏈結串列與 PDF 生成?
鏈結串列可以動態儲存和組織內容,隨後可以進行迭代,並使用像 IronPDF 這樣的庫將其轉換為 PDF 文件,促進內容的操作和輸出。
在軟體開發中使用鏈結串列的優勢是什麼?
鏈結串列提供高效的插入和刪除、動態調整大小,並且對於實現動態資料結構如堆疊和佇列非常有益。儘管它們缺少隨機存取能力,但在需要頻繁修改時特別有用。
單鏈結串列和雙向鏈結串列之間有什麼區別?
主要區別在於單鏈結串列的節點只有一個指向下一個節點的參考,允許單向遍歷,而雙向鏈結串列的節點有指向下一個和前一個節點的參考,允許雙向遍歷。
如何在 C# 中從鏈結串列資料生成 PDF?
可以迭代鏈結串列以收集資料,然後使用 IronPDF 的 API 將這些資料渲染為 PDF 文件。這涉及使用像 HtmlToPdf 這樣的方法將結構化內容轉換為專業的 PDF 格式。



