Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
A linked list is a linear data structure composed of a series of nodes, which can also be called as elements. Unlike arrays, where elements/nodes are stored in contiguous memory locations, linked lists utilize dynamic memory allocation, allowing elements/nodes to be scattered throughout memory.
In its simplest form, "linked lists" consist of nodes linked together linearly. Each node contains two main parts:
The last node in a linked list typically points to a null reference, indicating the end of the list.
In this article, we will look in detail at the Linked list in C# and also IronPDF, a PDF generation library from Iron Software.
Singly linked list has a node with only one reference, typically pointing to the next node in the sequence. Traversing the list is limited to moving in one direction, typically from the head (the initial node) to the tail (the final node).
In a doubly linked list, each node contains two references: one pointing to the next node and another pointing to the previous node in the sequence. This bidirectional linkage enables traversal in both forward and backward directions.
In a circular linked list, the last node points back to the first node, forming a circular structure. This type of linked list can be implemented using either singly or doubly linked nodes.
In C#, you can implement a linked list using the LinkedList
class from the System.Collections.Generic
namespace. Here's an example of all the basic operations:
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
LinkedList<int>()
foreach
loopFind
and AddAfter
methodsIronPDF is a powerful C# PDF library developed and maintained by Iron Software. It provides a comprehensive set of features for creating, editing, and extracting content from PDF documents within .NET projects.
IronPDF allows you to convert HTML content to PDF format. You can render HTML pages, URLs, and HTML strings into PDFs with ease.
The library offers a user-friendly API that enables developers to generate professional-quality PDFs directly from HTML. Whether you need to create invoices, reports, or other documents, IronPDF simplifies the process.
IronPDF is compatible with various .NET environments, including .NET Core, .NET Standard, and .NET Framework. It runs on Windows, Linux, and macOS platforms.
IronPDF supports different project types, such as web applications (Blazor and WebForms), desktop applications (WPF and MAUI), and console applications.
You can generate PDFs from various content sources, including HTML files, Razor views (Blazor Server), CSHTML (MVC and Razor), ASPX (WebForms), and XAML (MAUI).
IronPDF adheres to PDF standards, including versions 1.2 to 1.7, PDF/UA, and PDF/A. It also supports UTF-8 character encoding, base URLs, and asset encoding.
LinkedList
Now let's create a PDF document using IronPDF and also demo the usage of LinkedList
strings.
To start with, open Visual Studio and create a console application by selecting from project templates as shown below.
Provide a project name and location.
Select the required .NET version.
Install IronPDF from the Visual Studio Package manager like the one below.
Or can be installed using the below command line.
dotnet add package IronPdf --version 2024.4.2
Add the below code.
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
LinkedList<string>()
.AddAfter
method and print the result list.ChromePdfRenderer
, RenderHtmlAsPdf
, and SaveAs
methods.The output has a watermark which can be removed using a valid license from the licensing page.
IronPDF library requires a license to run.
A trial license is available on the trial license page.
Paste the Key in the appSettings.json file below.
{
"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
C# LinkedList
provides a versatile data structure for managing collections of elements, offering efficient insertions and deletions while accommodating dynamic resizing similar to the default hash function. Linked lists are commonly used in various applications and algorithms, such as implementing stacks, queues, symbol tables, and memory management systems. Understanding the characteristics and operations of linked lists is essential for building efficient and scalable software solutions.
In summary, while linked lists excel in certain scenarios, such as dynamic data structures and frequent insertions/deletions, they may not be the best choice for applications requiring frequent random access or dealing with memory-constrained environments. Careful consideration of the specific requirements and characteristics of the data can guide the selection of the most appropriate data structure for the task at hand.
IronPDF library from Iron Software to read and generate PDF documents, helps developers gain advanced skills to develop modern applications.
9 .NET API products for your office documents