🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。
🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。
🏆🎉欢迎 👍点赞✍评论⭐收藏
数据结构是计算机科学中的一个重要概念,它描述了数据之间的组织方式和关系,以及对这些数据的访问和操作。常见的数据结构有:数组、链表、栈、队列、哈希表、树、堆和图。
队列是一种线性数据结构,它的基本思想是先进先出(First In First Out,FIFO),即最先进入队列的元素最先被删除。
队列主要包括两个基本操作:入队和出队。入队操作就是将元素插入到队列的尾部,而出队操作则是删除队列的第一个元素。实现队列可以使用数组或链表等不同的数据结构,一般用数组实现的队列称为顺序队列,用链表实现的队列称为链式队列。
队列的应用场景非常广泛,如消息队列、进程调度、路由算法等。队列可以保证先进入队列的元素先被处理,具有较好的时间复杂度和空间复杂度,是一种非常实用的数据结构。
C#中队列的常用操作包括:
以下是C#中使用队列的示例:
using System;
using System.Collections;
class Program
{
static void Main(string[] args)
{
Queue myQueue = new Queue();
// 向队列中添加元素
myQueue.Enqueue("C#");
myQueue.Enqueue("Java");
myQueue.Enqueue("Python");
// 获取队列中元素的数量
Console.WriteLine("队列中元素的数量:{0}", myQueue.Count);
// 获取并移除队列中的第一个元素
string firstElement = (string)myQueue.Dequeue();
Console.WriteLine("第一个元素是:{0}", firstElement);
// 获取队列中的第一个元素,但不移除该元素
string peekedElement = (string)myQueue.Peek();
Console.WriteLine("队列中的第一个元素是:{0}", peekedElement);
// 遍历队列中的元素
Console.WriteLine("队列中的元素:");
foreach (string element in myQueue)
{
Console.WriteLine(element);
}
}
}
输出结果:
队列中元素的数量:3
第一个元素是:C#
队列中的第一个元素是:Java
队列中的元素:
Java
Python
ConcurrentQueue和Queue的区别
public void Queue()
{
var test = new ConcurrentQueue<TestModel>();//安全队列
//var test = Channel.CreateBounded<TestModel>(int.MaxValue);//管道
//var test = new Stack<TestModel>();//栈
//var test = new Queue<TestModel>();//队列
for (int i = 0; i < 1_000_000; i++)
{
test.Enqueue(new TestModel());
//test.Writer.TryWrite(new TestModel());
//test.Push(new TestModel());
//test.Enqueue(new TestModel());
}
}
public void QueueIO()
{
var test = new ConcurrentQueue<TestModel>();
//var test = Channel.CreateBounded<TestModel>(int.MaxValue);
//var test = new Stack<TestModel>();
//var test = new Queue<TestModel>();
for (int i = 0; i < 1_000_000; i++)
{
test.Enqueue(new TestModel());
//test.Writer.TryWrite(new TestModel());
//test.Push(new TestModel());
//test.Enqueue(new TestModel());
}
for (int i = 0; i < 1_000_000; i++)
{
test.TryDequeue(out var _);
//test.Reader.TryRead(out var _);
//test.Pop();
//test.Dequeue();
}
}
ConcurrentQueue和Queue的性能差别是纳米级大部分情况下都是使用ConcurrentQueue
图解:
示例:
/* 基于链表实现的队列 */
class LinkedListQueue {
private ListNode? front, rear; // 头节点 front ,尾节点 rear
private int queSize = 0;
public LinkedListQueue() {
front = null;
rear = null;
}
/* 获取队列的长度 */
public int size() {
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty() {
return size() == 0;
}
/* 入队 */
public void push(int num) {
// 尾节点后添加 num
ListNode node = new ListNode(num);
// 如果队列为空,则令头、尾节点都指向该节点
if (front == null) {
front = node;
rear = node;
// 如果队列不为空,则将该节点添加到尾节点后
} else if (rear != null) {
rear.next = node;
rear = node;
}
queSize++;
}
/* 出队 */
public int pop() {
int num = peek();
// 删除头节点
front = front?.next;
queSize--;
return num;
}
/* 访问队首元素 */
public int peek() {
if (size() == 0 || front == null)
throw new Exception();
return front.val;
}
/* 将链表转化为 Array 并返回 */
public int[] toArray() {
if (front == null)
return Array.Empty<int>();
ListNode node = front;
int[] res = new int[size()];
for (int i = 0; i < res.Length; i++) {
res[i] = node.val;
node = node.next;
}
return res;
}
}
图解:
示例:
/* 基于环形数组实现的队列 */
class ArrayQueue {
private int[] nums; // 用于存储队列元素的数组
private int front; // 队首指针,指向队首元素
private int queSize; // 队列长度
public ArrayQueue(int capacity) {
nums = new int[capacity];
front = queSize = 0;
}
/* 获取队列的容量 */
public int capacity() {
return nums.Length;
}
/* 获取队列的长度 */
public int size() {
return queSize;
}
/* 判断队列是否为空 */
public bool isEmpty() {
return queSize == 0;
}
/* 入队 */
public void push(int num) {
if (queSize == capacity()) {
Console.WriteLine(" 队列已满");
return;
}
// 计算尾指针,指向队尾索引 + 1
// 通过取余操作,实现 rear 越过数组尾部后回到头部
int rear = (front + queSize) % capacity();
// 将 num 添加至队尾
nums[rear] = num;
queSize++;
}
/* 出队 */
public int pop() {
int num = peek();
// 队首指针向后移动一位,若越过尾部则返回到数组头部
front = (front + 1) % capacity();
queSize--;
return num;
}
/* 访问队首元素 */
public int peek() {
if (isEmpty())
throw new Exception();
return nums[front];
}
/* 返回数组 */
public int[] toArray() {
// 仅转换有效长度范围内的列表元素
int[] res = new int[queSize];
for (int i = 0, j = front; i < queSize; i++, j++) {
res[i] = nums[j % this.capacity()];
}
return res;
}
}
队列是一种先进先出(FIFO)的数据结构,它的优点和缺点如下:
优点:
缺点:
队列是一种常见的数据结构,其应用场景如下:
在实际开发中,队列还有其他很多的应用场景,使用队列可以方便地实现很多数据结构和算法。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。