.NET 帮助

C# 链表(开发者如何工作)

发布 2024年六月6日
分享:

链接表是一种线性数据结构,由一系列节点(也可称为元素)组成。与元素/节点存储在连续内存位置的数组不同,链表利用动态内存分配,允许元素/节点分散在整个内存中。

最简单的 "链接列表 "由线性链接在一起的节点组成。每个节点包含两个主要部分:

  1. 数据:存储在节点中的有效载荷。根据实现的不同,可以是任何数据类型,如整数、字符串、对象等。

  2. 下一个指针:一个引用 (或指针) 指向序列中的下一个节点。该指针指示链接列表中下一个节点的内存位置。

链表中的最后一个节点通常指向一个空引用,表示链表的结束。

在本文中,我们将详细介绍 C# 中的链接表,以及 IronPDF的 PDF 生成库 铁软件.

关联列表的类型

1.单链表

单链表的节点只有一个引用,通常指向序列中的下一个节点。遍历列表仅限于朝一个方向移动,通常是从头部 (初始节点) 到尾巴 (最后一个节点).

2.双向链接列表

在双链表中,每个节点都包含两个引用:一个指向下一个节点,另一个指向序列中的上一个节点。这种双向链接可以实现向前和向后两个方向的遍历。

3.循环链接表

在循环链接表中,最后一个节点指向第一个节点,形成一个循环结构。这种类型的链表可以使用单链节点或双链节点来实现。

关联列表的基本操作

  1. 插入:在列表的特定位置(如开头、结尾或中间)添加一个新节点。

  2. 删除:从列表中删除指定对象节点,并相应调整相邻节点的指针。

  3. 遍历:遍历列表以访问或操作每个节点的数据。

  4. 搜索:根据数据指定值在列表中查找特定节点。

C&num 中的链接列表;

在 C# 中,您可以使用 链接列表 类。下面是所有基本操作的示例:

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 which create objects from node class
            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);
            }
            Console.WriteLine($"Number of Linked List elements:{linkedList.Count}"); // use count property to display length
            // Find/Search Element in Linked List
            Console.WriteLine("\nFind/Search Element Linked List elements: 30");
            var foundNode = linkedList.Find(30);// method returns the node
            Console.WriteLine($"Found Value:{foundNode.Value}, Next Element:{foundNode.Next.Value}, Previous Element:{foundNode.Previous.Value}"); // prints next node, previous node
            // Insert an element at a specified node
            LinkedListNode<int> current = linkedList.Find(20);
            linkedList.AddAfter(current, 25);
            Console.WriteLine($"\nNumber of Linked List elements:{linkedList.Count}"); // use count property to display length
            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); // remove current node
            Console.WriteLine("\nLinked List elements after removal:");
            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine($"\nNumber of Linked List elements:{linkedList.Count}"); // use count property to display length
        }
    }
}
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 which create objects from node class
            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);
            }
            Console.WriteLine($"Number of Linked List elements:{linkedList.Count}"); // use count property to display length
            // Find/Search Element in Linked List
            Console.WriteLine("\nFind/Search Element Linked List elements: 30");
            var foundNode = linkedList.Find(30);// method returns the node
            Console.WriteLine($"Found Value:{foundNode.Value}, Next Element:{foundNode.Next.Value}, Previous Element:{foundNode.Previous.Value}"); // prints next node, previous node
            // Insert an element at a specified node
            LinkedListNode<int> current = linkedList.Find(20);
            linkedList.AddAfter(current, 25);
            Console.WriteLine($"\nNumber of Linked List elements:{linkedList.Count}"); // use count property to display length
            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); // remove current node
            Console.WriteLine("\nLinked List elements after removal:");
            foreach (var item in linkedList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine($"\nNumber of Linked List elements:{linkedList.Count}"); // use count property to display length
        }
    }
}
Imports Microsoft.VisualBasic

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 which create objects from node class
			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
			Console.WriteLine($"Number of Linked List elements:{linkedList.Count}") ' use count property to display length
			' Find/Search Element in Linked List
			Console.WriteLine(vbLf & "Find/Search Element Linked List elements: 30")
			Dim foundNode = linkedList.Find(30) ' method returns the node
			Console.WriteLine($"Found Value:{foundNode.Value}, Next Element:{foundNode.Next.Value}, Previous Element:{foundNode.Previous.Value}") ' prints next node, previous node
			' Insert an element at a specified node
			Dim current As LinkedListNode(Of Integer) = linkedList.Find(20)
			linkedList.AddAfter(current, 25)
			Console.WriteLine($vbLf & "Number of Linked List elements:{linkedList.Count}") ' use count property to display length
			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) ' remove current node
			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}") ' use count property to display length
	End Sub
End Class
End Namespace
VB   C#

代码解释

1.使用新的 LinkedList 创建一个新的整数链接列表()`

2.在链接列表中添加指定值对象

3.使用 foreach 循环遍历并打印链表元素

4.查找/搜索链接列表中的元素

5.使用 FindAddAfter 方法在指定节点上插入元素

6.使用 Remove 方法从链表中删除现有节点

输出

C# 链接表(如何为开发人员工作):图 1 - 链接表输出

IronPDF 简介

IronPDF 是由 Iron Software 开发和维护的一个功能强大的 C# PDF 库。它为在 .NET 项目中创建、编辑和提取 PDF 文档内容提供了一套全面的功能。

关于 IronPDF 的要点

HTML 转换为 PDF

IronPDF 允许您将 HTML 内容转换为 PDF 格式。您可以轻松地将 HTML 页面、URL 和 HTML 字符串转换为 PDF。

丰富的应用程序接口

该库提供用户友好的 API,使开发人员能够直接从 HTML 生成专业品质的 PDF。无论您需要创建发票、报告还是其他文档,IronPDF 都能简化这一过程。

跨平台支持

IronPDF 兼容各种 .NET 环境,包括 .NET Core、.NET Standard 和 .NET Framework。它可在 Windows、Linux 和 macOS 平台上运行。

多功能

IronPDF 支持不同的项目类型,如网络应用程序 (Blazor 和 WebForms)桌面应用程序 (WPF 和 MAUI)和控制台应用程序。

内容来源

您可以从各种内容源生成 PDF,包括 HTML 文件、Razor 视图 (Blazor 服务器), CSHTML (MVC 和 Razor), ASPX (网络表格)和 XAML (毛伊岛).

附加功能

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,从项目模板中选择创建一个控制台应用程序,如下图所示。

C# 链接列表(如何为开发人员工作):图 2 - 新项目

提供项目名称和地点。

C# 链接列表(如何为开发人员工作):图 3 - 项目配置

选择所需的 .NET 版本。

C# 链接列表(如何为开发人员工作):图 4 - 目标框架

从 Visual Studio 软件包管理器中安装 IronPDF,如下所示。

C# 链接列表(如何为开发人员工作):图 5 - 安装 IronPDF

也可使用以下命令行进行安装。

dotnet add package IronPdf --version 2024.4.2

添加以下代码。

using 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></p>";
            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>";
            // Print the elements of the linked list
            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>";
            // Insert an element at a specific position
            LinkedListNode<string> node = linkedList.Find("Banana");
            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>";
            // Remove an element from the linked list
            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 Renderer
            var renderer = new ChromePdfRenderer();
            // Create a PDF from HTML string
            var pdf = renderer.RenderHtmlAsPdf(content);
            // Save to a file or Stream
            pdf.SaveAs("AwesomeIronOutput.pdf");        
    }
}
using 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></p>";
            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>";
            // Print the elements of the linked list
            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>";
            // Insert an element at a specific position
            LinkedListNode<string> node = linkedList.Find("Banana");
            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>";
            // Remove an element from the linked list
            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 Renderer
            var renderer = new ChromePdfRenderer();
            // Create a PDF from HTML string
            var pdf = renderer.RenderHtmlAsPdf(content);
            // Save to a file or Stream
            pdf.SaveAs("AwesomeIronOutput.pdf");        
    }
}
Imports Microsoft.VisualBasic
Imports 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></p>"
			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>"
			' Print the elements of the linked list
			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>"
			' Insert an element at a specific position
			Dim node As LinkedListNode(Of String) = linkedList.Find("Banana")
			linkedList.AddAfter(node, "Mango")
			content &= "<p>Find Banana and insert Mango After</p>"
			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>"
			' Remove an element from the linked list
			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 Renderer
			Dim renderer = New ChromePdfRenderer()
			' Create a PDF from HTML string
			Dim pdf = renderer.RenderHtmlAsPdf(content)
			' Save to a file or Stream
			pdf.SaveAs("AwesomeIronOutput.pdf")
	End Sub
End Class
VB   C#

代码解释

1.首先,我们使用内容字符串对象为 PDF 创建内容。内容以 HTML 字符串的形式创建。

2.创建一个新的字符串链接列表,使用新的 LinkedList<string>().

3.在链接列表和 PDF 内容字符串中添加元素。

4.打印链接列表中的元素

5.使用 AddAfter 方法在特定位置插入一个元素,并打印结果列表。

6.使用 Remove 方法从链表中删除一个元素,并打印结果列表。

7.最后,使用 ChromePdfRendererRenderHtmlAsPdfSaveAs 方法将生成的 HTML 内容字符串保存为 PDF 文档。

输出

C# 链接列表(如何为开发人员工作):图 6 - IronPDF 的 链接列表 输出

输出结果带有水印,可以使用有效的许可证去除水印。 许可页面.

IronPDF 许可证

IronPDF 库需要许可证才能运行。

试用许可证可在 试用许可证页面.

将密钥粘贴到下面的 appSettings.json 文件中。

{
  "IronPdf.License.LicenseKey" = "The Key Goes Here"
}
{
  "IronPdf.License.LicenseKey" = "The Key Goes Here"
}
If True Then
  "IronPdf.License.LicenseKey" = "The Key Goes Here"
End If
VB   C#

结论

C# LinkedList 为管理元素集合提供了一种通用的数据结构,在提供高效的插入和删除功能的同时,还能适应类似于默认哈希函数的动态大小调整。链接表常用于各种应用程序和算法,如实现堆栈、队列、符号表和内存管理系统。了解链接表的特性和操作对于构建高效、可扩展的软件解决方案至关重要。

总之,虽然链接表在某些情况下(如动态数据结构和频繁插入/删除)表现出色,但对于需要频繁随机访问或处理内存受限环境的应用程序来说,它们可能不是最佳选择。仔细考虑数据的具体要求和特征,可以为手头的任务选择最合适的数据结构。

Iron Software 的 IronPDF 库可以读取和生成 PDF 文档,帮助开发人员获得开发现代应用程序的高级技能。

< 前一页
C# 反转字符串(开发者如何使用)
下一步 >
C# iList(它如何为开发人员工作)

准备开始了吗? 版本: 2024.9 刚刚发布

免费NuGet下载 总下载量: 10,731,156 查看许可证 >