.NET ヘルプ

C# リンクドリスト (開発者向けの仕組み)

更新済み 6月 6, 2024
共有:

リンクリストは、ノードの一連の構成から成る線形データ構造であり、これらは要素とも呼ばれます。 配列では要素/ノードが連続したメモリ位置に保存されますが、リンクリストでは動的メモリ割り当てを利用するため、要素/ノードはメモリ全体に散在することができます。

最も単純な形では、「リンクリスト」は線形にリンクされたノードで構成されます。 各ノードは2つの主要部分を含みます:

  1. データ: ノードに格納されているペイロード。 これは実装によって任意のデータ型である可能性があります。例えば整数、文字列、オブジェクトなどです。

  2. ネクストポインター:参照 (またはポインター) 次のシーケンスのノードへ。 このポインタは、リンクリスト内で次のノードのメモリ位置を指し示します。

    リンクリストの最後のノードは通常、ヌル参照を指し、リストの終わりを示します。

    この記事では、C#のリンクリストについて詳細に見ていきます。また IronPDFIronPDF, a PDF generation library from Iron Software アイアンソフトウェア.

連結リストの種類

単方向リスト

単方向リストは、通常シーケンス内の次のノードを指す1つの参照のみを持つノードから成ります。 リストをトラバースすることは、一方向に移動することに限られており、通常はヘッドからになります。 (初期ノード) テールまで (最終ノード).

ダブリリンクリスト

二重リンクリストでは、各ノードに2つの参照が含まれています。一つは次のノードを指し、もう一つはシーケンス内の前のノードを指します。 この双方向リンクは、前方向および後方向の両方でのトラバーサルを可能にします。

循環リンクリスト

循環連結リストでは、最後のノードが最初のノードに指し戻され、円形の構造を形成します。 この種類のリンクリストは、単方向または双方向リンクノードを使用して実装することができます。

リンクドリストの基本操作

  1. 挿入:リストの特定の位置(例:先頭、末尾、中間)に新しいノードを追加すること。

  2. 削除: 指定されたオブジェクトノードをリストから削除し、隣接するノードのポインタを適切に調整すること。

  3. トラバーサル: リストを反復して各ノードのデータにアクセスまたは操作すること。

  4. 検索:データ指定値に基づいてリスト内の特定のノードを見つけること。

C#におけるリンクリスト

C# では、リンクリストを使用して実装できます LinkedList 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 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. 指定されたノードに要素を挿入するには、Find メソッドおよび AddAfter メソッドを使用します。

  6. 削除メソッドを使用してリンクリストから既存のノードを削除する

出力

C# リンクリスト(開発者向けの動作方法):図1 - リンクリスト出力

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)およびコンソールアプリケーション。

コンテンツソース

さまざまなコンテンツソースからPDFを生成することができ、HTMLファイルやRazorビューを含みます。 (Blazor サーバー), 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を開き、以下のようにプロジェクトテンプレートからコンソールアプリケーションを作成します。

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 を使用して新しい文字列のリンクリストを作成します。()`.

  3. リンクリストに要素を追加し、PDFコンテンツ文字列にも追加します。

  4. 連結リストの要素を印刷する。

  5. AddAfterメソッドを使用して特定の位置に要素を挿入し、結果のリストを表示します。

  6. Removeメソッドを使用してリンクリストから要素を削除し、結果のリストを出力します。

  7. 最後に、生成されたHTMLコンテンツ文字列をChromePdfRendererRenderHtmlAsPdf、およびSaveAsメソッドを使用してPDFドキュメントとして保存します。

出力

C# リンクトリスト (開発者向けの動作方法): 図6 - IronPDFと`LinkedList`の出力

出力には透かしが含まれていますが、有効なライセンスを使用することで削除できます。 ライセンスページ.

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,659,073 View Licenses >