当前位置:网站首页>C# 自定义Queue队列集合
C# 自定义Queue队列集合
2022-07-23 05:45:00 【LKZღ木子李】
队列集合,先进先出
性能跟C#自带的差不多
实现代码如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LKZ
{
/// <summary>
/// 队列
/// </summary>
[Serializable]
public class Queue<T> :
ICollection<T>, IEnumerable<T>, IEnumerable, IEnumerator, IEnumerator<T>, IReadOnlyCollection<T>
{
/// <summary>
/// 数组的容量
/// </summary>
public int Capacity {
get; private set; }
/// <summary>
/// 数组现在有多少个元素
/// </summary>
public int Count {
get; private set; }
public bool IsReadOnly => false;
T IEnumerator<T>.Current => this.Dequeue();
object IEnumerator.Current => this.Dequeue();
/// <summary>
/// 数组
/// </summary>
private T[] array;
/// <summary>
/// 第一个元素的位置
/// </summary>
private int first;
/// <summary>
/// 最后一个元素的位置
/// </summary>
private int last;
public Queue()
: this(10)
{
}
/// <summary>
/// 构造
/// </summary>
/// <param name="capacity">初始容量的大小</param>
public Queue(int capacity)
{
array = new T[capacity];
Capacity = capacity;
}
/// <summary>
/// 构造
/// </summary>
/// <param name="ienum">开始给队列添加元素,是一个迭代器</param>
public Queue(IEnumerable<T> ienum)
: this(ienum.Count())
{
foreach (var item in ienum)
{
Enqueue(item);
}
}
/// <summary>
/// 移除并返回第一位元素
/// </summary>
/// <returns></returns>
public T Dequeue()
{
if (Count == 0)
ThrowError("数组为空,没有办法获得第一个数组");
//缩容
if (Count <= Capacity / 4)
{
ResetArray(Capacity / 2);
}
Count--;
T temp = array[first];
array[first] = default(T);
first = ++first % Capacity;
return temp;
}
/// <summary>
/// 再尾部添加元素
/// </summary>
/// <param name="item">需要添加的元素</param>
public void Enqueue(T item)
{
//扩容
if (Count == Capacity)
{
ResetArray(Capacity * 2);
}
Count++;
array[last] = item;
last = ++last % Capacity;
}
/// <summary>
/// 看一下第一个元素,不移除
/// </summary>
/// <returns></returns>
public T Peek()
{
if (Count == 0)
ThrowError("数组为空,没有办法获得第一个数组");
return array[first];
}
/// <summary>
/// 重置数组的大小容量
/// </summary>
/// <param name="capacity">新的数组的大小容量</param>
private void ResetArray(int capacity)
{
T[] newArray = new T[capacity];
if (Count > 0)
{
if (first < last)
{
Array.Copy(array, first, newArray, 0, Count);
}
else
{
int temp = this.Capacity - first;
Array.Copy(array, first, newArray, 0, temp);
Array.Copy(array, 0, newArray, temp, last);
}
}
first = 0;
last = Count;
this.Capacity = capacity;
array = newArray;
}
private void ThrowError(string message)
{
throw new ArgumentException(message);
}
void ICollection<T>.Add(T item)
{
Enqueue(item);
}
void ICollection<T>.Clear()
{
Array.Clear(array, 0, Capacity);
first = 0;
last = 0;
Count = 0;
}
/// <summary>
/// 是否包含这个元素
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
public bool Contains(T item)
{
int tempFirst = first;
for (int i = 0; i < Count; i++)
{
if (array[tempFirst].Equals(item))
{
return true;
}
else
{
tempFirst = ++tempFirst & Capacity;
}
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
if (object.ReferenceEquals(array, null))
ThrowError("复制到数组为空");
else if (arrayIndex >= Capacity)
ThrowError("开始复制的数组索引超出Array的大小");
else if (arrayIndex < 0)
ThrowError("开始复制的数组索引不能小于0");
int tempFirst = first;
for (int i = 0; i < Count; i++)
{
array[i] = this.array[tempFirst];
tempFirst = ++tempFirst % Capacity;
}
}
public bool Remove(T item)
{
ThrowError("队列不支持此方法");
return false;
}
public IEnumerator<T> GetEnumerator()
{
return this;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this;
}
public void Dispose()
{
}
public bool MoveNext()
{
return Count > 0;
}
public void Reset()
{
}
}
}
下面是性能对比
进行90000000添加数据
Stopwatch stopwatch1 = new Stopwatch();
stopwatch1.Start();
Queue<int> queue = new Queue<int>();//自己的集合
for (int i = 0; i < 90000000; i++)
{
queue.Enqueue(i);
}
stopwatch1.Stop();
Console.WriteLine("使用时间:" + stopwatch1.ElapsedMilliseconds + " 元素个数" + queue.Count );
stopwatch1.Restart();
System.Collections.Generic.Queue<int> queue1 = new System.Collections.Generic.Queue<int>();//c#的集合
for (int i = 0; i < 90000000; i++)
{
queue1.Enqueue(i);
}
Console.WriteLine("使用时间:" + stopwatch1.ElapsedMilliseconds + " 元素个数" + queue1.Count);
性能结果如下:
上面就是性能对比结果!
关注微信公众号【浪子独白】 获得更多精彩内容!
边栏推荐
- 0动态规划 LeetCode918. 环形子数组的最大和
- Question bank of basic principles of steel structure
- Dynamic programming - "coin exchange problem"
- 广播,组播,单播
- (1)ASIO
- Mutual implementation of queue and heap (pure C implementation)
- Problems encountered in configuring the historical version of detectron
- Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 1
- 剑*offer—— 链表逆序
- Unity3D+moba+技能指示器(一)
猜你喜欢

Blog building five: drawing bed selection
![[physical layer of CAN bus] 1. Content sharing of can/canfd sampling points](/img/e4/0b709a6ed5e639a75e0506f6eac9fd.png)
[physical layer of CAN bus] 1. Content sharing of can/canfd sampling points
![[AUTOSAR candrive 2. understand the mapping relationship between communication HOH, canid and pduid]](/img/6d/ae145053b5fc46b583e5892ca4a945.png)
[AUTOSAR candrive 2. understand the mapping relationship between communication HOH, canid and pduid]

二叉树基础oj练习-

C语言数据库:基于tcp多进程的在线词典,有详细的步骤已经图解,欢迎大家来观看

Unity3d+GameFramework:资源分析,资源依赖,循环依赖检测

Question bank of basic principles of steel structure

Analysis of 100 questions and answers in Higher Algebra

常见的排序—交换排序

Steel structure review questions
随机推荐
Embedded from entry to mastery (buried) - sharing of ultra detailed knowledge points 1
Unity在URP管线下使用TriLib插件加载模型材质不正确的问题
求矩阵的鞍点及其对应的下标。
Flask项目中创建数据库表db.create_all()
[AUTOSAR cantp 1. learn the network layer protocol of UDS diagnosis]
1. Ten principles of Economics
Blog building I: Framework selection
Summary of video coding and decoding related data
Blog Building II: next theme related settings beta
C语言数据库:详细的说明用学生管理系统了解数据库的操作,简单易懂。
0动态规划 LeetCodde313. 超级丑数
嵌入式从入门到精通(入土)——超详细知识点分享2
[AUTOSAR DEM iv.event memory]
视频编解码相关资料汇总
桌面远程协议-编解码
Unity3d+GameFramework:资源分析,资源依赖,循环依赖检测
VS属性配置相关知识
Blog Building III: comment system selection
3.2daydayup举一反三:三天打鱼两天晒网式学习
vscode配置