二、队列
队列先进先出,一头进一头出,用Queue<T>实现
[Serializable]
[DebuggerTypeProxy(typeof(System_QueueDebugView<>))]
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable
可以看出队列实现了集合的接口,迭代的接口
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
Queue<String> queue = new Queue<string>();
//进队
queue.Enqueue("张三");
queue.Enqueue("李四");
queue.Enqueue("王五");
queue.Enqueue("田六");
queue.Enqueue("赵七");
foreach (String item in queue)
{
Console.WriteLine("foreach迭代:" + item);
}
//出队
while (queue.Count > 0)
{
Console.WriteLine("出队:" + queue.Dequeue());
}
Console.Read();
}
}
}
三、栈
栈:从同一边先进后出,用Stack<T>实现
[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(System_StackDebugView<>))]
[ComVisible(false)]
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable
栈也是实现了集合接口与迭代接口的
using System;
using System.Collections.Generic;
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
Stack<String> stack = new Stack<string>();
//进栈
stack.Push("张三");
stack.Push("李四");
stack.Push("王五");
stack.Push("田六");
stack.Push("赵七");
foreach (String item in stack)
{
Console.WriteLine("foreach迭代:" + item);
}
//出栈
while (stack.Count > 0)
{
Console.WriteLine("出栈:" + stack.Pop());
}
Console.Read();
}
}
}
