C# Linked List(開発者向けの動作方法)
連結リストは、一連のノードで構成された線形データ構造であり、要素とも呼ばれることがあります。 要素/ノードが連続したメモリ位置に格納される配列と異なり、連結リストは動的メモリ割り当てを利用し、要素/ノードをメモリ全体に分散させます。
最も単純な形では、"連結リスト"は互いに線形にリンクするノードで構成されています。 各ノードには2つの主要な部分があります。
- データ: ノードに格納されるペイロード。 これは整数、文字列、オブジェクトなど、実装によってさまざまなデータ型にすることができます。
- 次のポインタ: シーケンス内の次のノードへの参照(またはポインタ)。 このポインタは、連結リストの次のノードが配置されているメモリ位置を指し示します。
連結リストの最後のノードは通常、リストの終わりを示すヌル参照を指します。
この記事では、C#の連結リストを詳細に見て、IronPDFライブラリ、Iron SoftwareによるPDF生成ツールも探求します。
連結リストの種類
1. 単方向連結リスト
単方向連結リストは、シーケンス内の次のノードを指す通常1つの参照を持つノードで構成されます。 リストの移動は、通常はヘッド(初めのノード)からテール(最後のノード)への一方通行に制限されます。
2. 双方向連結リスト
双方向連結リストでは、各ノードが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
ライブラリは、開発者がHTMLから直接プロフェッショナル品質のPDFを生成するためのユーザーフレンドリーなAPIを提供します。 請求書、レポート、その他の文書を作成するにせよ、IronPDFはプロセスを簡略化します。
クロスプラットフォームサポート
IronPDFは.NET Core、.NET Standard、および.NET Frameworkを含む様々な.NET環境と互換性があります。 それはWindows、Linux、およびmacOSプラットフォームで動作します。
多様性
IronPDFは、Webアプリケーション(BlazorおよびWebForms)、デスクトップアプリケーション(WPFおよびMAUI)、コンソールアプリケーションなど、さまざまなプロジェクトタイプをサポートしています。
コンテンツのソース
HTMLファイル、Razorビュー(Blazorサーバー)、CSHTML(MVCおよびRazor)、ASPX(WebForms)、XAML(MAUI)など、さまざまなコンテンツソースからPDFを生成できます。
追加機能
- PDFにヘッダーやフッターを追加します。
- PDFページをマージ、分割、追加、コピー、および削除します。
- パスワード、権限、およびデジタル署名を設定します。
- マルチスレッドおよび非同期サポートを利用してパフォーマンスを最適化します。
互換性
IronPDFは、バージョン1.2から1.7、PDF/UA、およびPDF/Aを含むPDF標準に準拠しています。 また、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メソッドを使用して特定の位置に要素を挿入します。 update content and print the resulting list.Removeメソッドを使用して連結リストから要素を削除して、コンテンツを更新し、結果のリストを印刷します。 - 最後に、生成されたHTMLコンテンツ文字列を
ChromePdfRenderer、RenderHtmlAsPdf、およびSaveAsメソッドを使用して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形式に変換することを含みます。








