隊列(Queue)表明了一個先進先出的對象集合。當您須要對各項進行先進先出的訪問時,則使用隊列。當您在列表中添加一項,稱爲入隊,當您從列表中移除一項時,稱爲出隊。數組
下表列出了 Queue 類的一些經常使用的 屬性:spa
屬性 | 描述 |
---|---|
Count | 獲取 Queue 中包含的元素個數。 |
下表列出了 Queue 類的一些經常使用的 方法:code
序號 | 方法名 & 描述 |
---|---|
1 | public virtual void Clear(); 從 Queue 中移除全部的元素。 |
2 | public virtual bool Contains( object obj ); 判斷某個元素是否在 Queue 中。 |
3 | public virtual object Dequeue(); 移除並返回在 Queue 的開頭的對象。 |
4 | public virtual void Enqueue( object obj ); 向 Queue 的末尾添加一個對象。 |
5 | public virtual object[] ToArray(); 複製 Queue 到一個新的數組中。 |
6 | public virtual void TrimToSize(); 設置容量爲 Queue 中元素的實際個數。 |
下面的實例演示了隊列(Queue)的使用:對象
1 using System; 2 using System.Collections; 3 4 namespace CollectionsApplication 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Queue q = new Queue(); 11 12 q.Enqueue('A'); 13 q.Enqueue('M'); 14 q.Enqueue('G'); 15 q.Enqueue('W'); 16 17 Console.WriteLine("Current queue: "); 18 foreach (char c in q) 19 Console.Write(c + " "); 20 Console.WriteLine(); 21 q.Enqueue('V'); 22 q.Enqueue('H'); 23 Console.WriteLine("Current queue: "); 24 foreach (char c in q) 25 Console.Write(c + " "); 26 Console.WriteLine(); 27 Console.WriteLine("Removing some values "); 28 char ch = (char)q.Dequeue(); 29 Console.WriteLine("The removed value: {0}", ch); 30 ch = (char)q.Dequeue(); 31 Console.WriteLine("The removed value: {0}", ch); 32 Console.ReadKey(); 33 } 34 } 35 }
當上面的代碼被編譯和執行時,它會產生下列結果:blog
Current queue: A M G W Current queue: A M G W V H Removing values The removed value: A The removed value: M
堆棧(Stack)表明了一個後進先出的對象集合。當您須要對各項進行後進先出的訪問時,則使用堆棧。當您在列表中添加一項,稱爲推入元素,當您從列表中移除一項時,稱爲彈出元素。隊列
下表列出了 Stack 類的一些經常使用的 屬性:rem
屬性 | 描述 |
---|---|
Count | 獲取 Stack 中包含的元素個數。 |
下表列出了 Stack 類的一些經常使用的 方法:string
序號 | 方法名 & 描述 |
---|---|
1 | public virtual void Clear(); 從 Stack 中移除全部的元素。 |
2 | public virtual bool Contains( object obj ); 判斷某個元素是否在 Stack 中。 |
3 | public virtual object Peek(); 返回在 Stack 的頂部的對象,但不移除它。 |
4 | public virtual object Pop(); 移除並返回在 Stack 的頂部的對象。 |
5 | public virtual void Push( object obj ); 向 Stack 的頂部添加一個對象。 |
6 | public virtual object[] ToArray(); 複製 Stack 到一個新的數組中。 |
下面的實例演示了堆棧(Stack)的使用:it
1 using System; 2 using System.Collections; 3 4 namespace CollectionsApplication 5 { 6 class Program 7 { 8 static void Main(string[] args) 9 { 10 Stack st = new Stack(); 11 12 st.Push('A'); 13 st.Push('M'); 14 st.Push('G'); 15 st.Push('W'); 16 17 Console.WriteLine("Current stack: "); 18 foreach (char c in st) 19 { 20 Console.Write(c + " "); 21 } 22 Console.WriteLine(); 23 24 st.Push('V'); 25 st.Push('H'); 26 Console.WriteLine("The next poppable value in stack: {0}", 27 st.Peek()); 28 Console.WriteLine("Current stack: "); 29 foreach (char c in st) 30 { 31 Console.Write(c + " "); 32 } 33 Console.WriteLine(); 34 35 Console.WriteLine("Removing values "); 36 st.Pop(); 37 st.Pop(); 38 st.Pop(); 39 40 Console.WriteLine("Current stack: "); 41 foreach (char c in st) 42 { 43 Console.Write(c + " "); 44 } 45 } 46 } 47 }
當上面的代碼被編譯和執行時,它會產生下列結果:io
Current stack: W G M A The next poppable value in stack: H Current stack: H V W G M A Removing values Current stack: G M A