C# 연결 리스트 (개발자에게 어떻게 작동하는가)
연결 리스트는 일련의 노드를 구성 요소로 하는 선형 데이터 구조로, 요소라고도 불립니다. 배열과 달리 요소/노드가 인접 메모리 위치에 저장되는 것이 아니라, 연결 리스트는 동적 메모리 할당을 사용하여 메모리 전역에 분산될 수 있습니다.
가장 단순한 형태로, "연결 리스트"는 선형으로 함께 연결된 노드들로 구성됩니다. 각 노드는 두 개의 주요 부분을 포함합니다:
- 데이터: 노드에 저장된 페이로드. 이는 구현에 따라 정수, 문자열, 객체 등 어떤 데이터 유형도 될 수 있습니다.
- 다음 포인터: 시퀀스의 다음 노드를 가리키는 참조(또는 포인터). 이 포인터는 연결 리스트에서 다음 노드의 메모리 위치를 앞으로 가리킴을 나타냅니다.
연결 리스트의 마지막 노드는 일반적으로 리스트의 끝을 나타내는 null 참조를 가리킵니다.
이 기사에서는 C#의 연결 리스트를 자세히 살펴보고 Iron Software의 PDF 생성 도구인 IronPDF 라이브러리도 탐구할 것입니다.
연결 리스트의 종류
1. 단일 연결 리스트
단일 연결 리스트는 시퀀스의 다음 노드를 가리키는 하나의 참조만 가지는 노드를 갖습니다. 리스트를 순회하는 것은 한 방향으로만 이동이 제한되며, 일반적으로 머리(초기 노드)에서 꼬리(마지막 노드)로 이동하는 방식입니다.
2. 이중 연결 리스트
이중 연결 리스트에서는 각 노드가 두 개의 참조를 가지고 있으며, 하나는 다음 노드를 가리키고 다른 하나는 시퀀스의 이전 노드를 가리킵니다. 이 양방향 연결은 앞뒤 방향으로 양방향 순회를 가능하게 합니다.
3. 원형 연결 리스트
원형 연결 리스트에서 마지막 노드는 첫 번째 노드로 다시 포인팅하여 원형 구조를 형성합니다. 이 유형의 연결 리스트는 단일 또는 이중 연결 노드를 사용하여 구현할 수 있습니다.
연결 리스트의 기본 작업
- 삽입: 특정 위치에 새로운 노드를 리스트에 추가합니다, 시작, 끝 또는 중간 같은 위치입니다.
- 삭제: 리스트에서 지정된 객체 노드를 제거하고 인접 노드의 포인터를 적절히 조정합니다.
- 순회: 각 노드의 데이터를 접근하거나 조작하기 위해 리스트를 반복합니다.
- 검색: 데이터 지정 값에 기반하여 리스트에서 특정 노드를 찾습니다.
Linked List in C
C#에서는 System.Collections.Generic 네임스페이스의 LinkedList 클래스를 사용하여 연결 리스트를 구현할 수 있습니다. 여기 모든 기본 연산의 예시가 있습니다:
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 Core, .NET Standard, .NET Framework를 포함한 다양한 .NET 환경과 호환됩니다. Windows, Linux 및 macOS 플랫폼에서 실행됩니다.
다재다능함
IronPDF는 웹 애플리케이션(Blazor 및 WebForms), 데스크톱 애플리케이션(WPF 및 MAUI), 콘솔 애플리케이션과 같은 다양한 프로젝트 유형을 지원합니다.
콘텐츠 소스
HTML 파일, Razor 뷰(Blazor Server), CSHTML(MVC 및 Razor), ASPX(WebForms), XAML(MAUI) 등 다양한 콘텐츠 소스로부터 PDF를 생성할 수 있습니다.
추가 기능
- PDF에 헤더와 푸터를 추가합니다.
- PDF 페이지를 병합, 분할, 추가, 복사 및 삭제합니다.
- 암호, 권한 및 디지털 서명을 설정합니다.
- 멀티스레딩 및 비동기 지원으로 성능을 최적화합니다.
호환성
IronPDF는 PDF 버전 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 System
Imports System.Collections.Generic
Imports IronPdf
Namespace CsharpSamples
Public Class Program
Public Shared Sub Main()
Dim content As String = "<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(Of 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
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(vbCrLf & "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
content += "<h2>Remove an element from the linked list</h2>"
linkedList.Remove("Orange")
content += "<p>Remove Orange from linked list</p>"
Console.WriteLine(vbCrLf & "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
' Create a PDF renderer
Dim renderer As 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 형식으로 변환하는 것을 포함합니다.




