【玩转 AtomCode】源码编译与架构解析:从 Rust 工程到 AI 编码代理内核的 TaoToken 接入实践
2026/10/16 0:07:19
队列(Queue)是计算机科学中一种重要的数据结构,它遵循"先进先出"(FIFO)原则。在C#中,System.Collections.Queue类提供了队列的实现。本教程将全面介绍C#中队列的使用方法。
队列是一种线性数据结构,具有以下特点:
在C#中,可以使用Queue类来创建队列:
usingSystem;usingSystem.Collections;classProgram{staticvoidMain(){// 创建一个空队列Queuequeue=newQueue();// 使用泛型版本(推荐)Queue<string>stringQueue=newQueue<string>();}}推荐使用泛型版本Queue<T>,因为它提供了类型安全性和更好的性能。
使用Enqueue方法向队列尾部添加元素:
Queue<string>tasks=newQueue<string>();tasks.Enqueue("任务1");tasks.Enqueue("任务2");tasks.Enqueue("任务3");使用Dequeue方法移除并返回队列开头的元素:
stringfirstTask=tasks.Dequeue();// 返回"任务1"Console.WriteLine(firstTask);使用Peek方法查看队列开头的元素但不移除它:
stringnextTask=tasks.Peek();// 返回"任务2"但不移除Console.WriteLine(nextTask);使用Count属性获取队列中的元素数量:
Console.WriteLine($"队列中还有{tasks.Count}个任务");usingSystem;usingSystem.Collections.Generic;classQueueExample{staticvoidMain(){// 创建队列Queue<string>messageQueue=newQueue<string>();// 添加消息messageQueue.Enqueue("第一条消息");messageQueue.Enqueue("第二条消息");messageQueue.Enqueue("第三条消息");Console.WriteLine($"队列初始大小:{messageQueue.Count}");// 处理消息while(messageQueue.Count>0){stringmessage=messageQueue.Dequeue();Console.WriteLine($"处理中:{message}");}Console.WriteLine($"处理后队列大小:{messageQueue.Count}");// 尝试查看空队列的Peektry{stringemptyPeek=messageQueue.Peek();}catch(InvalidOperationExceptionex){Console.WriteLine($"错误:{ex.Message}");}}}虽然队列设计为FIFO结构,但可以使用foreach循环遍历:
Queue<int>numbers=newQueue<int>();numbers.Enqueue(1);numbers.Enqueue(2);numbers.Enqueue(3);foreach(intnuminnumbers){Console.WriteLine(num);}// 注意:遍历不会移除元素// 转换为数组int[]numberArray=numbers.ToArray();// 转换为列表List<int>numberList=newList<int>(numbers);在多线程环境中,可以使用ConcurrentQueue<T>:
usingSystem.Collections.Concurrent;ConcurrentQueue<int>concurrentQueue=newConcurrentQueue<int>();concurrentQueue.Enqueue(10);concurrentQueue.Enqueue(20);if(concurrentQueue.TryDequeue(outintresult)){Console.WriteLine(result);}Queue<Action>taskQueue=newQueue<Action>();// 添加任务taskQueue.Enqueue(()=>Console.WriteLine("任务A执行"));taskQueue.Enqueue(()=>Console.WriteLine("任务B执行"));// 执行任务while(taskQueue.Count>0){vartask=taskQueue.Dequeue();task.Invoke();}usingSystem;usingSystem.Collections.Generic;classNode{publicintValue{get;set;}publicList<Node>Children{get;set;}=newList<Node>();}classProgram{staticvoidBFS(Noderoot){if(root==null)return;Queue<Node>queue=newQueue<Node>();queue.Enqueue(root);while(queue.Count>0){Nodecurrent=queue.Dequeue();Console.Write(current.Value+" ");foreach(varchildincurrent.Children){queue.Enqueue(child);}}}staticvoidMain(){// 构建树结构Noderoot=newNode{Value=1};root.Children.Add(newNode{Value=2});root.Children.Add(newNode{Value=3});root.Children[0].Children.Add(newNode{Value=4});root.Children[0].Children.Add(newNode{Value=5});Console.WriteLine("广度优先遍历结果:");Bfs(root);// 输出: 1 2 3 4 5}}Queue<T>的Enqueue和Dequeue操作都是O(1)时间复杂度CircularBuffer等自定义实现| 特性 | 队列(Queue) | 栈(Stack) |
|---|---|---|
| 顺序 | 先进先出(FIFO) | 后进先出(LIFO) |
| 添加方法 | Enqueue | Push |
| 移除方法 | Dequeue | Pop |
| 查看方法 | Peek | Peek |
| 典型应用 | 任务调度、BFS | 函数调用、撤销操作 |
C#中的队列(Queue<T>)是一种简单但强大的数据结构,适用于需要先进先出处理的场景。通过本教程,你应该已经掌握了: