C# 中的数据结构

作者:微信公众号:【架构师老卢】
6-8 20:26
51

概述:介绍任何软件应用程序的基础、数据结构都会影响其性能、可扩展性和效率。全面掌握和熟练掌握数据结构可以大大提高 C# 编程的质量。在这本内容丰富的书中,我们将探讨几种 C# 数据结构,包括实现、用法和最佳实践。完成本课程后,您将具备在 C# 项目中有效利用数据结构所需的专业知识。数据结构 — 数组:在 C# 中,数组是最基本和最基本的数据结构之一。它们提供了一种简单的方法,用于以给定尺寸存储相同类型部件的顺序集合。由于数组是许多其他复杂数据结构和算法的基础,因此您必须了解它们。声明和初始化:在 C# 中,可以通过多种方式声明和初始化数组:// Declaration and initializat

介绍

任何软件应用程序的基础、数据结构都会影响其性能、可扩展性和效率。全面掌握和熟练掌握数据结构可以大大提高 C# 编程的质量。在这本内容丰富的书中,我们将探讨几种 C# 数据结构,包括实现、用法和最佳实践。完成本课程后,您将具备在 C# 项目中有效利用数据结构所需的专业知识。

数据结构 — 数组:

在 C# 中,数组是最基本和最基本的数据结构之一。它们提供了一种简单的方法,用于以给定尺寸存储相同类型部件的顺序集合。由于数组是许多其他复杂数据结构和算法的基础,因此您必须了解它们。

声明和初始化:

在 C# 中,可以通过多种方式声明和初始化数组:

// Declaration and initialization of an array with a specified size  
int[] numbers = new int[5]; // Creates an array of integers with a length of 5  
// Declaration and initialization of an array with initial values  
int[] numbers = { 1, 2, 3, 4, 5 }; // Creates an array of integers with initial values

访问元素:

访问数组中的元素是使用从零开始的索引完成的:

int[] numbers = { 1, 2, 3, 4, 5 };  
int firstElement = numbers[0]; // Accesses the first element (1)  
int thirdElement = numbers[2]; // Accesses the third element (3)

遍历数组:

可以使用循环(例如)对数组进行迭代,例如:forforeach

int[] numbers = { 1, 2, 3, 4, 5 };  
// Using for loop  
for (int i = 0; i < numbers.Length; i++)  
{  
    Console.WriteLine(numbers[i]);  
}  
// Using foreach loop  
foreach (int num in numbers)  
{  
    Console.WriteLine(num);  
}

修改元素:

可以通过为特定索引分配新值来修改数组中的元素:

int[] numbers = { 1, 2, 3, 4, 5 };  
numbers[2] = 10; // Modifies the third element to be 10

Length 属性:

该属性提供数组中的元素数:

int[] numbers = { 1, 2, 3, 4, 5 };  
int arrayLength = numbers.Length; // Returns 5

数据结构 — 列表:

C# 中的列表是动态数据结构,可灵活地管理元素集合。与数组不同,列表的大小可以动态增大或缩小,因此适用于事先不知道元素数或可能随时间变化的场景。

声明和初始化:

列表是命名空间的一部分,可以按如下方式声明和初始化:

// Declaration and initialization of a list  
List<int> numbers = new List<int>(); // Creates an empty list of integers  
// Initialization of a list with initial values  
List<string> names = new List<string>() { "Alice", "Bob", "Charlie" }; // Creates a list of strings with initial values

添加元素:

可以使用以下方法将元素添加到列表中:

List<int> numbers = new List<int>();  
numbers.Add(1); // Adds 1 to the list  
numbers.Add(2); // Adds 2 to the list

访问元素:

列表中的元素可以通过索引访问:

List<string> names = new List<string>() { "Alice", "Bob", "Charlie" };  
string firstElement = names[0]; // Accesses the first element ("Alice")

遍历列表:

可以使用循环(例如)对列表进行迭代,例如:forforeach

List<string> names = new List<string>() { "Alice", "Bob", "Charlie" };  
// Using for loop  
for (int i = 0; i < names.Count; i++)  
{  
    Console.WriteLine(names[i]);  
}  
// Using foreach loop  
foreach (string name in names)  
{  
    Console.WriteLine(name);  
}

删除元素:

可以使用 、 或 等方法从列表中删除元素:

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };  
numbers.Remove(3); // Removes the element 3 from the list  
numbers.RemoveAt(0); // Removes the element at index 0 (the first element)  
numbers.Clear(); // Removes all elements from the list

Count 属性:

该属性提供列表中的元素数:

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5 };  
int listCount = numbers.Count; // Returns 5

数据结构 — 堆栈:

堆栈是计算机科学中遵循后进先出 (LIFO) 原则的基本数据结构。在 C# 中,该类位于命名空间中,它提供了一个集合,该集合允许以后进先出的方式添加和删除元素。

声明和初始化:

您可以使用以下类在 C# 中创建堆栈:

Stack<int> stack = new Stack<int>();

推送操作:

该方法用于将元素添加到堆栈的顶部:Push

stack.Push(1);  
stack.Push(2);  
stack.Push(3);

完成这些操作后,堆栈将包含 .{ 3, 2, 1 }

弹出操作:

该方法用于从堆栈中删除和返回顶部元素:

int poppedElement = stack.Pop(); // Returns 3 and removes it from the stack

操作完成后,堆栈将包含 .Pop{ 2, 1 }

窥视操作:

该方法用于在不删除堆栈的情况下查看堆栈的顶部元素:Peek

int topElement = stack.Peek(); // Returns 2 (the top element) without removing it from the stack

操作完成后,堆栈保持不变:。Peek{ 2, 1 }

检查堆栈是否为空:

您可以使用该属性来检查堆栈是否为空:

if (stack.Count == 0)  
{  
    Console.WriteLine("Stack is empty");  
}

用法示例:

堆栈的一个常见用例是实现算法,例如表达式计算、回溯和管理递归中的函数调用。

Stack<int> stack = new Stack<int>();  
stack.Push(1);  
stack.Push(2);  
stack.Push(3);  
while (stack.Count > 0)  
{  
    int topElement = stack.Pop();  
    Console.WriteLine(topElement);  
}

数据结构 — 队列:

队列是计算机科学中常用的另一种基本数据结构,它遵循先进先出 (FIFO) 原则。在 C# 中,该类位于命名空间中,它提供了一个集合,该集合允许以先进先出的方式添加和删除元素。

声明和初始化:

可以使用以下类在 C# 中创建队列:

Queue<string> queue = new Queue<string>();

排队操作:

该方法用于将元素添加到队列的后面:

queue.Enqueue("Task 1");  
queue.Enqueue("Task 2");  
queue.Enqueue("Task 3");

执行这些操作后,队列将包含 .{ "Task 1", "Task 2", "Task 3" }

取消排队操作:

该方法用于从队列中删除和返回 front 元素:

string dequeuedTask = queue.Dequeue(); // Returns "Task 1" and removes it from the queue

操作完成后,队列将包含 .Dequeue{ "Task 2", "Task 3" }

窥视操作:

该方法用于在不删除队列的情况下查看队列的前元素:

string frontTask = queue.Peek(); // Returns "Task 2" (the front element) without removing it from the queue

操作完成后,队列保持不变:.Peek{ "Task 2", "Task 3" }

检查队列是否为空:

您可以使用该属性来检查队列是否为空:

if (queue.Count == 0)  
{  
    Console.WriteLine("Queue is empty");  
}

用法示例:

队列的一个常见用例是在任务计划、广度优先搜索和 Web 服务器中管理请求等方案中。

Queue<int> queue = new Queue<int>();  
queue.Enqueue(1);  
queue.Enqueue(2);  
queue.Enqueue(3);  
while (queue.Count > 0)  
{  
    int frontElement = queue.Dequeue();  
    Console.WriteLine(frontElement);  
}
阅读排行