C#链表(开发人员如何使用)
链表是一种线性数据结构,由一系列节点组成,也可以称为元素。 与数组不同,数组中的元素/节点存储在连续的内存位置,链表利用动态内存分配,允许元素/节点在内存中散布。
在其最简单的形式中,"链表"由线性链接在一起的节点组成。 每个节点包含两个主要部分:
- 数据:存储在节点中的有效负载。 这可以是任意数据类型,具体取决于实现,例如整数、字符串、对象等。
- 下一指针:指向序列中下一个节点的引用(或指针)。 该指针指示链表中下一个节点的内存位置。
链表中的最后一个节点通常指向一个空引用,指示列表的结束。
在本文中,我们将详细探讨 C# 中的链表,并探索 IronPDF 库,这是来自 Iron Software 的 PDF 生成工具。
链表类型
1. 单链表
单链表的节点只有一个引用,通常指向序列中的下一个节点。 遍历列表仅限于一个方向移动,通常从头(初始节点)到尾(最终节点)。
2. 双链表
在双链表中,每个节点包含两个引用:一个指向下一个节点,另一个指向序列中的上一个节点。 这种双向链接允许在前向和后向方向上进行遍历。
3. 循环链表
在循环链表中,最后一个节点指向第一个节点,形成循环结构。 这种类型的链表可以使用单链节点或双链节点实现。
链表的基本操作
- 插入:在特定位置(如开头、结尾或中间)添加新节点到列表。
- 删除:从列表中删除指定的对象节点,并相应地调整相邻节点的指针。
- 遍历:通过列表迭代,以访问或操作每个节点的数据。
- 搜索:根据其数据指定的值查找列表中的特定节点。
C# 中的链表
在 C# 中,您可以使用 LinkedList 类(来自 System.Collections.Generic 命名空间)实现链表。 以下是所有基本操作的示例:
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>()创建一个整数的链表。 - 将指定值对象添加到链表中。
- 使用
foreach循环遍历并打印链表的元素。 - 查找/搜索链表中的元素。
- 使用
Find和AddAfter方法在指定节点处插入元素。 - 使用
Remove方法从链表中删除现有节点。
输出

IronPDF 简介
了解更多关于 IronPDF 是由 Iron Software 开发和维护的强大的 C# PDF 库。 它提供了全面的功能集,用于在 .NET 项目中创建、编辑和提取 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 支持不同的项目类型,例如 Web 应用程序(Blazor 和 WebForms)、桌面应用程序(WPF 和 MAUI)和控制台应用程序。
内容来源
您可以从各种内容来源生成 PDF,包括 HTML 文件、Razor 视图(Blazor Server)、CSHTML(MVC 和 Razor)、ASPX(WebForms)和 XAML(MAUI)。
附加功能
- 为 PDF 添加页眉和页脚。
- 合并、拆分、添加、复制和删除 PDF 页面。
- 设置密码、权限和数字签名。
- 通过多线程和异步支持优化性能。
兼容性
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代码解释
- 首先,我们通过创建一个内容字符串对象来开始生成 PDF 的内容。 内容以 HTML 字符串的形式生成。
- 使用
new LinkedList<string>()创建一个字符串的链表。 - 向链表添加元素,并将信息附加到 PDF 内容字符串中。
- 打印链表的元素,并将其附加到 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 格式。








