這篇文章主要講解了“C#數(shù)據(jù)結(jié)構(gòu)與隊(duì)列怎么實(shí)現(xiàn)”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“C#數(shù)據(jù)結(jié)構(gòu)與隊(duì)列怎么實(shí)現(xiàn)”吧!
創(chuàng)新互聯(lián)公司IDC提供業(yè)務(wù):重慶服務(wù)器托管,成都服務(wù)器租用,重慶服務(wù)器托管,重慶服務(wù)器租用等四川省內(nèi)主機(jī)托管與主機(jī)租用業(yè)務(wù);數(shù)據(jù)中心含:雙線機(jī)房,BGP機(jī)房,電信機(jī)房,移動(dòng)機(jī)房,聯(lián)通機(jī)房。
C#數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列是一種特殊的線性表,它只允許在表的前端(front)進(jìn)行刪除操作,而在表的后端(back)進(jìn)行插入操作。進(jìn)行插入操作的端稱為隊(duì)尾,進(jìn)行刪除操作的端稱為隊(duì)頭。這也就是我們平常經(jīng)常用說(shuō)到的先進(jìn)先出法則(FIFO),隊(duì)列這種法則,在中國(guó)好久以前就開始運(yùn)用了,例如糧倉(cāng)管理官員,在沒(méi)掌握這種法則前,倉(cāng)庫(kù)底部的糧食都因時(shí)間太久而壞掉了,后來(lái)有聰明人士在糧倉(cāng)二邊開個(gè)門,一邊進(jìn)倉(cāng)一邊出倉(cāng),這樣管理就方便多了。隊(duì)列中沒(méi)有元素時(shí),稱為空隊(duì)列。
C#數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列實(shí)現(xiàn)的接口如下:
public interface IQueen﹤T﹥ { int Length(); bool IsEmpty(); bool IsFull(); void Clear(); void IN(T items); T Out(); T GetFrontItem(); }
C#數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列實(shí)現(xiàn)的原理與代碼如下:
public class JQueen﹤T﹥ : IQueen﹤T﹥ { private int size; private T[] item; private int front; private int back; public JQueen() : this(100) { size = 100; item = new T[100]; front = back = -1; } public JQueen(int length) { size = length; item = new T[length]; front = back = -1; } public T this[int index] { get { return item[index]; } set { item[index] = value; } } public int Front { get { return front; } set { front = value; } } public int Back { get { return back; } set { back = value; } } public int MaxLength { get { return size; } set { size = value; } } public int Length() { return (back - front + size) % size; } public bool IsEmpty() { return (front == back); } public bool IsFull() { return ((back + 1) % size == front); } public void Clear() { front = back = -1; } public void IN(T items) { if (IsFull()) { throw new ArgumentOutOfRangeException("RangeException", "Queen RangeException: queen is full"); } item[++back] = items; } public T Out() { T tmp = default(T); if (IsEmpty()) { throw new ArgumentOutOfRangeException("RangeException", "Queen RangeException: queen is empty"); } tmp = item[++front]; return tmp; } public T GetFrontItem() { if (IsEmpty()) { throw new ArgumentOutOfRangeException("RangeException", "Queen RangeException: queen is empty"); } return item[front + 1]; } }
C#數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列的測(cè)試隊(duì)列代碼:
public class Program { static void Main(string[] args) { try { JQueen﹤string﹥ JQ = new JQueen﹤string﹥(); Console.WriteLine(JQ.IsEmpty()); //是否為空 Console.WriteLine(JQ.IsFull()); //是否滿隊(duì) Console.WriteLine(JQ.MaxLength); //初始化時(shí)隊(duì)列的長(zhǎng)度 Console.WriteLine(JQ.Length()); //隊(duì)列元素長(zhǎng)度 Console.WriteLine(JQ.Front); //隊(duì)頭位置 Console.WriteLine(JQ.Back); //隊(duì)尾位置 JQ.IN("A"); //插入元素 JQ.IN("B"); JQ.IN("C"); JQ.IN("D"); Console.WriteLine(JQ.GetFrontItem()); //隊(duì)頭元素 Console.WriteLine("------元素出隊(duì)后隊(duì)頭元素-------"); JQ.Out(); //出A JQ.Out(); Console.WriteLine(JQ.GetFrontItem()); //出隊(duì)二個(gè)元素后隊(duì)頭元素 Console.ReadLine(); } catch (Exception ex) { Console.WriteLine(ex.Message); //異常 Console.ReadLine(); } } }
C#數(shù)據(jù)結(jié)構(gòu)與算法之隊(duì)列程序運(yùn)行結(jié)果如下:
感謝各位的閱讀,以上就是“C#數(shù)據(jù)結(jié)構(gòu)與隊(duì)列怎么實(shí)現(xiàn)”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)C#數(shù)據(jù)結(jié)構(gòu)與隊(duì)列怎么實(shí)現(xiàn)這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!