概述
看了一個數據結構的教程,是用C++寫的,可自己C#還是一個菜鳥,更別說C++了,但還是大膽嘗試用C#將其中的環形隊列的實現寫出來,先上代碼:
1 public class MyQueue<T> : IDisposable 2 { 3 private T[] queue; 4 private int length; 5 private int capacity; 6 private int head = 0; 7 private int tail = 0; 8 9 public MyQueue(int capacity) { 10 this.capacity = capacity; 11 this.head = 0; 12 this.tail = 0; 13 this.length = 0; 14 this.queue = new T[capacity]; 15 } 16 17 public void Clear() { 18 head = 0; 19 tail = 0; 20 length = 0; 21 } 22 23 public bool IsEmpty() { 24 return length == 0; 25 } 26 27 public bool IsFull() { 28 return length == capacity; 29 } 30 31 public int Length() { 32 return length; 33 } 34 35 public bool EnQueue(T node) { 36 if (!IsFull()) { 37 queue[tail] = node; 38 tail = (++tail) % capacity; 39 length++; 40 return true; 41 } 42 return false; 43 } 44 45 public T DeQueue() { 46 T node = default(T); 47 if (!IsEmpty()) { 48 node = queue[head]; 49 head = (++head) % capacity; 50 length--; 51 } 52 return node; 53 } 54 55 public void Traverse() { 56 for (int i = head; i < length + head; i++) { 57 Console.WriteLine(queue[i % capacity]); 58 Console.WriteLine($"前面還有{i - head}個"); 59 } 60 } 61 62 public void Dispose() { 63 queue = null; 64 } 65 }
為了能夠通用,所以用的是泛型來實現環形隊列類。這里最重要的是進隊(EnQueue)和出隊(DeQueue)兩個方法,進隊或出隊后頭和尾的位置都要通過取模運算來獲得,因為是環形隊列嘛,你懂的。
一、簡單類型隊列
好了,測試下入隊:
1 class Program 2 { 3 static void Main(string[] args) { 4 MyQueue<int> queue = new MyQueue<int>(4); 5 queue.EnQueue(10); 6 queue.EnQueue(16); 7 queue.EnQueue(18); 8 queue.EnQueue(12); 9 queue.Traverse(); 10 Console.Read(); 11 } 12 }
顯示結果:

再測試下出隊:
1 class Program 2 { 3 static void Main(string[] args) { 4 MyQueue<int> queue = new MyQueue<int>(4); 5 queue.EnQueue(10); 6 queue.EnQueue(16); 7 queue.EnQueue(18); 8 queue.EnQueue(12); 9 queue.Traverse(); 10 11 Console.WriteLine("彈兩個出去"); 12 queue.DeQueue(); 13 queue.DeQueue(); 14 Console.WriteLine(); 15 queue.Traverse(); 16 Console.Read(); 17 } 18 }
運行結果:
二、復雜類型隊列
之前也說了,這個隊列類是用的泛型寫的,對應於C++的模板了,那就意味着任何類型都可以使用這個隊列類,來測試個自定義的類試試,如下先定義一個Customer類:
1 public class Customer 2 { 3 public string Name { get; set; } 4 5 public int Age { get; set; } 6 7 public void PringInfo() { 8 Console.WriteLine("姓名:" + Name); 9 Console.WriteLine("年齡:" + Age); 10 Console.WriteLine(); 11 } 12 }
然后進行入隊,如下:
1 class Program 2 { 3 static void Main(string[] args) { 4 MyQueue<Customer> queue = new MyQueue<Customer>(5); 5 queue.EnQueue(new Customer() { Name = "宋小二", Age = 29 }); 6 queue.EnQueue(new Customer() { Name = "陳小三", Age = 28 }); 7 queue.EnQueue(new Customer() { Name = "王小四", Age = 26 }); 8 queue.EnQueue(new Customer() { Name = "朱小五", Age = 48 }); 9 for (int i = 0; i < queue.Length(); i++) { 10 queue[i].PringInfo(); 11 } 12 Console.Read(); 13 } 14 }
上面的代碼 queue[i].PringInfo();是通過索引來實現,所以我們得在隊列類中實現索引,添加如下代碼到MyQueue.cs類中,如下:
1 public T this[int index] { 2 get { 3 return queue[index]; 4 } 5 }
感覺用for循環來遍歷還是不夠好,想用foreach,那就給MyQueue類加個遍歷接口,如下:

然后實現這個接口,如下:
1 public IEnumerator<T> GetEnumerator() { 2 foreach(T node in queue) { 3 if(node != null) { 4 yield return node; 5 } 6 } 7 } 8 9 IEnumerator IEnumerable.GetEnumerator() { 10 return GetEnumerator(); 11 }
這樣遍歷的地方就可以改成foreach了,如下:


執行結果:
總結:
編程的思想才是最重要的,無關語言。
因為自己的水平有限,所以寫的代碼都很簡單,如果哪寫的不好或需要改進的,希望路過的高手不吝指教,如果能
推薦就更完美了,謝謝閱讀。
