写在前面
最近在使用.net 开发一些程序。 它使用的编程语言是C#。 我们来看一下它的常用的数据结构有哪些。
常用数据结构
C# 中常见的数据结构:
1数组 (Array):用于存储固定大小的同类型元素集合。
2列表 (List):动态大小的数组,可以随时添加或删除元素。
3链表 (LinkedList):由节点组成的集合,每个节点包含对下一个节点的引用,适合频繁插入和删除操作。
4集合 (HashSet):不允许重复元素的集合,使用哈希表实现,适合快速查找。
5字典 (Dictionary<TKey, TValue>):键值对集合,使用哈希表实现,适合根据键快速查找值。
6队列 (Queue):先进先出(FIFO)的数据结构,适合需要顺序处理的场景。
7栈 (Stack):先进后出(LIFO)的数据结构,适合后进先处理的场景。
8堆 (Heap):通常通过 PriorityQueue<TElement, TPriority> 实现,用于优先级队列。
9图 (Graph):可以使用邻接表或邻接矩阵表示,用于表示节点及其连接关系。
10集合 (SortedSet):自动排序且不允许重复元素的集合。
这些数据结构各有特点,适用于不同的场景和需求。在选择数据结构时,应根据具体情况考虑时间复杂度、空间复杂度和操作的频率等因素。
代码示例
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
// 1. 数组 (Array)
int[] scores = { 85, 90, 78, 92, 88 };
Console.WriteLine("学生成绩:");
foreach (var score in scores)
{
Console.WriteLine(score);
}
// 2. 列表 (List<T>)
List<string> students = new List<string>();
students.Add("Alice");
students.Add("Bob");
Console.WriteLine("\n学生名单:");
foreach (var student in students)
{
Console.WriteLine(student);
}
// 3. 链表 (LinkedList<T>)
LinkedList<string> playlist = new LinkedList<string>();
playlist.AddLast("Song A");
playlist.AddLast("Song B");
Console.WriteLine("\n播放列表:");
foreach (var song in playlist)
{
Console.WriteLine(song);
}
// 4. 集合 (HashSet<T>)
HashSet<string> interests = new HashSet<string>();
interests.Add("Music");
interests.Add("Sports");
interests.Add("Music"); // 不会添加重复的 "Music"
Console.WriteLine("\n兴趣标签:");
foreach (var interest in interests)
{
Console.WriteLine(interest);
}
// 5. 字典 (Dictionary<TKey, TValue>)
Dictionary<int, string> studentDict = new Dictionary<int, string>();
studentDict.Add(1, "Alice");
studentDict.Add(2, "Bob");
Console.WriteLine("\n学生字典:");
foreach (var entry in studentDict)
{
Console.WriteLine(#34;ID: {entry.Key}, 姓名: {entry.Value}");
}
// 6. 队列 (Queue<T>)
Queue<string> queue = new Queue<string>();
queue.Enqueue("Customer 1");
queue.Enqueue("Customer 2");
Console.WriteLine("\n排队服务:");
while (queue.Count > 0)
{
Console.WriteLine(#34;服务: {queue.Dequeue()}");
}
// 7. 栈 (Stack<T>)
Stack<string> backStack = new Stack<string>();
backStack.Push("Page 1");
backStack.Push("Page 2");
Console.WriteLine("\n浏览器后退功能:");
while (backStack.Count > 0)
{
Console.WriteLine(#34;后退到: {backStack.Pop()}");
}
// 8. 堆 (Heap)
PriorityQueue<string, int> taskQueue = new PriorityQueue<string, int>();
taskQueue.Enqueue("Task A", 2); // 优先级 2
taskQueue.Enqueue("Task B", 1); // 优先级 1
Console.WriteLine("\n任务处理顺序:");
while (taskQueue.Count > 0)
{
var task = taskQueue.Dequeue();
Console.WriteLine(#34;处理: {task}");
}
// 9. 图 (Graph)
Dictionary<string, List<string>> graph = new Dictionary<string, List<string>>();
graph["Alice"] = new List<string> { "Bob", "Charlie" };
graph["Bob"] = new List<string> { "Alice", "David" };
Console.WriteLine("\n社交网络图:");
foreach (var user in graph)
{
Console.WriteLine(#34;{user.Key} 的好友: {string.Join(", ", user.Value)}");
}
// 10. 集合 (SortedSet<T>)
SortedSet<string> sortedInterests = new SortedSet<string>();
sortedInterests.Add("Music");
sortedInterests.Add("Sports");
sortedInterests.Add("Art");
Console.WriteLine("\n排序的兴趣标签:");
foreach (var interest in sortedInterests)
{
Console.WriteLine(interest);
}
}
}代码说明:
- 数组、列表、链表、集合、字典、队列、栈、堆、图、集合的示例代码均包含在一个主程序中。
- 每个数据结构都包含了初始化、添加元素和遍历的操作。
- 输出结果将显示在控制台上,可以清楚地看到每种数据结构的内容。
你可以将上述代码复制到 C# 开发环境(如 Visual Studio 或 .NET CLI)中运行。
写在最后
对于现代语言, 提供基本数据结构的类库是标配。语言只是实现目标的工具。 熟悉一种语言, 换另外一种语言可以很快上手。 我之前做了10年的java,现在使用C#开发程序,语法不同而已。 关键是编程思想和具体业务, 使用那种语言倒是无所谓。
