C#實(shí)現(xiàn)泛型動(dòng)態(tài)循環(huán)數(shù)組隊(duì)列的方法
任務(wù)
循環(huán)數(shù)組
實(shí)現(xiàn)目標(biāo):(1)創(chuàng)建一個(gè)新的數(shù)組數(shù)據(jù)結(jié)構(gòu);
(2)該數(shù)據(jù)結(jié)構(gòu)為泛型;
?。?)可以按照元素多少進(jìn)行擴(kuò)容縮容;
(4)進(jìn)行添加刪除操作的時(shí)間復(fù)雜度小于O(n);
優(yōu)勢(shì):在取出放入的操作中消耗的資源更少
劣勢(shì):取出特定元素或特定下標(biāo)元素平均消耗的資源為普通數(shù)組平均消耗資源的最大值
循環(huán)數(shù)組隊(duì)列
實(shí)現(xiàn)目標(biāo):(1)根據(jù)循環(huán)數(shù)組構(gòu)建出循環(huán)的隊(duì)列數(shù)據(jù)結(jié)構(gòu)
優(yōu)勢(shì):節(jié)省資源,運(yùn)行速度快;
劣勢(shì):不能靈活取出
重點(diǎn):如何實(shí)現(xiàn)循環(huán)的計(jì)算下標(biāo)語(yǔ)句。
循環(huán)下標(biāo)語(yǔ)句
完整代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataStructrue { /// <summary> /// 循環(huán)數(shù)組 /// (1)添加功能 /// (2)刪除功能 /// (3)查詢(任何、首尾處)功能 /// (4)修改(任何,首位處)功能 /// </summary> /// <typeparam name="E"></typeparam> class Array2<E> { private E[] data; private int N; private int first; private int last; public Array2(int capacity) { data = new E[capacity]; N = 0; first = 0; last = 0; } public Array2() : this(10) { } public int Count { get { return N; } } public bool IsEmpty { get { return N==0; } } public E GetFirst() return data[first]; /// <summary> /// 添加一個(gè)元素 /// </summary> /// <param name="e"></param> public void Add(E e) if (N==data.Length) { ResetCapacity(data.Length*2); } data[last] = e; last = (last + 1) % data.Length; N++; /// 移除早放進(jìn)的一個(gè)元素 /// <returns></returns> public E RemoveLast() if (N==0) throw new ArgumentException("隊(duì)列已空"); if (N<=(data.Length/4)) ResetCapacity(data.Length / 2); E de = data[first]; data[first] = default; first = (first + 1) % data.Length; N--; return de; /// 移除特定下標(biāo)元素 /// 消耗大,不建議使用 /// <param name="index"></param> public E RemoveAt(int index) if (index > data.Length || index < 0 ||N==0) throw new ArgumentException("非法索引"); if (first > last) if (index < first && index >= last) { throw new ArgumentException("非法索引"); } else if (last > first) E rd = data[index]; for (int i = index+1; i !=last ; i=(i+1)%data.Length) data[i-1] = data[i]; last--; return rd; /// 移除特定元素 public E Remove(E e) for (int i = first; i !=last; i=(i+1)%data.Length) if (data[i].Equals(e)) return RemoveAt(i); return data[last]; /// 對(duì)數(shù)組進(jìn)行擴(kuò)容操作 /// <param name="newcapacity"></param> private void ResetCapacity(int newcapacity) E[] data2 = new E[newcapacity]; for (int i = 0; i < N; i++) data2[i] = data[first]; first = (first + 1) % data.Length; last = i+1; data = data2; public override string ToString() //實(shí)例化 StringBuilder res = new(); //重寫(xiě)格式1:輸出數(shù)組元素個(gè)數(shù)以及長(zhǎng)度 //res.Append(string.Format("Array1: count={0} capacity={1}\n",N,data.Length)); res.Append(string.Format("A2Queue: Count = {0} Capacity = {1}\n[", N, data.Length)); res.Append(data[(first+i)%data.Length]); if (i!=N-1) res.Append(','); res.Append(']'+"\n"); //返回 return res.ToString(); } }
補(bǔ)充:c#使用數(shù)組實(shí)現(xiàn)泛型隊(duì)列Quque<T>,以循環(huán)的方式使用數(shù)組提高性能
隊(duì)列簡(jiǎn)述
一種先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu)
本文主旨
- 提供一個(gè)確定容量的高性能隊(duì)列的實(shí)現(xiàn)
- 更進(jìn)一步可以對(duì)隊(duì)列做動(dòng)態(tài)擴(kuò)容,每次隊(duì)列滿了的時(shí)候增加隊(duì)列容量
- 隊(duì)列也可以使用鏈表實(shí)現(xiàn)
實(shí)現(xiàn)代碼
using System; namespace DataStructure { /// <summary> /// 用數(shù)組實(shí)現(xiàn)隊(duì)列 /// 用2個(gè)index標(biāo)記開(kāi)始合結(jié)束 /// </summary> /// <typeparam name="T"></typeparam> public class ArrayQueue<T> { private int mCapicity; private int mStartIndex; private int mEndIndex; private int mCount; private T[] mArray; public ArrayQueue(int capicity) { mCapicity = capicity; mArray = new T[capicity]; } public int Count get { return mCount; } public bool IsFull return mCount == mCapicity; public int Capicity get { return mCapicity; } public bool IsEmpty return mCount == 0; public void Clear() mStartIndex = 0; mEndIndex = 0; mCount = 0; mCapicity = 0; mArray = null; public void Enqueue(T e) //隊(duì)列滿了 if (IsFull) throw new Exception("queue is full"); mArray[mEndIndex] = e; mCount++; //計(jì)算下一個(gè)位置 mEndIndex++; if (mEndIndex == mCapicity) mEndIndex = 0; public T Dequeue() //隊(duì)列空 if (IsEmpty) throw new Exception("queue is empty"); var r = mArray[mStartIndex]; //計(jì)算下一次取元素的index //取出元素后增加start mStartIndex++; //到達(dá)尾部,開(kāi)始循環(huán),下一次從頭開(kāi)始取 if (mStartIndex == mCapicity) mStartIndex = 0; mCount--; return r; } }
測(cè)試代碼
namespace DataStructure { public class ArrayQueueTest : BaseSolution { public void Test() { var queue = new ArrayQueue<int>(4); queue.Enqueue(1); queue.Enqueue(2); queue.Enqueue(3); queue.Enqueue(4); // println(queue.Capicity); // println(queue.Count); println(queue.Dequeue()); queue.Enqueue(5); while (!queue.IsEmpty) { println(queue.Dequeue()); } } } }
到此這篇關(guān)于C#實(shí)現(xiàn)泛型動(dòng)態(tài)循環(huán)數(shù)組隊(duì)列的文章就介紹到這了,更多相關(guān)C#泛型動(dòng)態(tài)循環(huán)數(shù)組隊(duì)列內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺析C#?AsyncLocal如何在異步間進(jìn)行數(shù)據(jù)流轉(zhuǎn)
在異步編程中,處理異步操作之間的數(shù)據(jù)流轉(zhuǎn)是一個(gè)比較常用的操作,C#異步編程提供了一個(gè)強(qiáng)大的工具來(lái)解決這個(gè)問(wèn)題,那就是AsyncLocal,下面我們就來(lái)看看AsyncLocal的原理和用法吧2023-08-08c# NPOI 如何在指定單元格導(dǎo)入導(dǎo)出圖片
這篇文章主要介紹了c# NPOI 如何在指定單元格導(dǎo)入導(dǎo)出圖片,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03C#使用迭代法實(shí)現(xiàn)Fibnaci數(shù)列
這篇文章主要介紹了C#使用迭代法實(shí)現(xiàn)Fibnaci數(shù)列的方法,較為詳細(xì)的分析了Fibnaci數(shù)列的原理與迭代法實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05C#實(shí)現(xiàn)Winform動(dòng)態(tài)添加菜單的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Winform動(dòng)態(tài)添加菜單的方法,涉及C#操作菜單的技巧,需要的朋友可以參考下2015-05-05Unity報(bào)錯(cuò)InvalidOperationException: out of sync的解決
今天在做個(gè)東西,發(fā)現(xiàn)報(bào)錯(cuò),特此來(lái)記錄一下,本文介紹了Unity報(bào)錯(cuò)InvalidOperationException: out of sync的解決,感興趣的可以了解一下2021-05-05C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
這篇文章主要介紹了C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法,涉及C#實(shí)現(xiàn)單實(shí)例程序運(yùn)行的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09