队列
- 命名空间 System.Collections
- 特点 先进先出,能接受空值,并且允许重复的元素
- 主要操作
Enqueue() 将元素加至队列末尾 Dequeue() 从开头处移除第一个元素 Peek() 返回队列中的第一个元素,但不删除对头元素
- 属性
Count | 获取Queue中所包含的元素数 |
IsSynchronized | 获取一个值,该指表示是否同步对Queue的访问(线程安全) |
SyncRoot | 获取可用于同步对Queue访问的对象 |
- 方法
方法名 | 功能 |
---|---|
void Clear() | 移除Queue中所有的元素 |
bool Contains(object) | 判断某元素是否在队列之中 |
object Dequeue() | 移除Queue中的队头元素 |
void Enqueue(Object object) | 向队尾添加一个新的对象 |
object ToArray() | 复制Queue到一个新的数组 |
void TrimToSize() | 设置容量为Queue中的实际元素个数 |
- 实现
/*
*模拟匹配舞伴
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dance
{
class Program
{
static void Main(string[] args)
{
string[] gentlemen = new string[] { "男一", "男二", "男三", "男四", "男五", "男六", "男七" };
string[] ladies = new string[] { "女一", "女二", "女三", "女四", "女五" };
Queue<string> waitingGentlemen = new Queue<string>(); //男士等待队列
Queue<string> waitingLadies = new Queue<string>(); //女士等待队列
Queue<string> dancingGentlemen = new Queue<string>(); //男士跳舞队列
Queue<string> dancingLadies = new Queue<string>(); //女士跳舞队列
foreach (string gentleman in gentlemen)
{
waitingGentlemen.Enqueue(gentleman);//进入等待队列
}
foreach (string lady in ladies)
{
waitingLadies.Enqueue(lady);
}
int turns = 1;//舞曲场数
do
{
Console.Clear();
Console.WriteLine("第{0}场舞曲开始:", turns++);
while (waitingGentlemen.Count > 0 && waitingLadies.Count > 0)
{
string gentleman = waitingGentlemen.Dequeue();//从等待队列中出来一个男士
string lady = waitingLadies.Dequeue();
Console.WriteLine("{0}先生和{1}女士配成舞伴开始跳舞!", gentleman, lady);
dancingGentlemen.Enqueue(gentleman);
dancingLadies.Enqueue(lady);
}
//输出正在等待的序列
foreach (string gentleman in waitingGentlemen)
{
Console.WriteLine("{0}先生等待舞伴!", gentleman);
}
foreach (string lady in ladies)
{
Console.WriteLine("{0}女士等待舞伴!", lady);
}
Console.WriteLine("舞曲停止");
while (dancingGentlemen.Count > 0 && dancingLadies.Count > 0)
{
string gentleman = dancingGentlemen.Dequeue();
string lady = dancingLadies.Dequeue();
Console.WriteLine("{0}先生进入等待队列", gentleman);
Console.WriteLine("{0}女士进入等待队列", lady);
waitingGentlemen.Enqueue(gentleman);
waitingLadies.Enqueue(lady);
}
} while (Console.ReadLine()!="exit");
}
}
}
堆栈
- 命名空间:System.Collections
- 特点:表示对象后进先出的非泛型集合
- 构造函数
Stack() | 初始化Stack类的新实例,该实例为空且具有默认初始容量 |
Stack(Collection) | 初始化Stack类的新实例,实例包含从指定集合复制的元素并且具有与所复制的元素数相同的初始容量 |
Stack(Int32) | 始化 Stack 类的新实例,该实例为空并且具有指定的初始容量或默认初始容量(这两个容量中的较大者) |
- 方法
Clear() | |
Clone() | |
Contains(Object) | 确定某元素是否在 Stack 中。Determines whether an element is in the Stack. |
CopyTo(Array, Int32) | 从指定数组索引开始将 Stack 复制到现有一维 Array 中。Copies the Stack to an existing one-dimensional Array, starting at the specified array index. |
Equals(Object) | 确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetEnumerator() | 返回 IEnumerator 的 Stack。Returns an IEnumerator for the Stack. |
GetHashCode() | 作为默认哈希函数。Serves as the default hash function. (Inherited from Object) |
GetType() | 获取当前实例的 Type。Gets the Type of the current instance. (Inherited from Object) |
MemberwiseClone() | 创建当前 Object 的浅表副本。Creates a shallow copy of the current Object. (Inherited from Object) |
Peek() | 返回位于 Stack 顶部的对象但不将其移除。Returns the object at the top of the Stack without removing it. |
Pop() | 删除并返回 Stack 顶部的对象。Removes and returns the object at the top of the Stack. |
Push(Object) | 在 Stack 的顶部插入一个对象。Inserts an object at the top of the Stack. |
Synchronized(Stack) | 返回 Stack 的同步(线程安全)包装。Returns a synchronized (thread safe) wrapper for the Stack. |
ToArray() | |
ToString() | 返回表示当前对象的字符串。Returns a string that represents the current object. (Inherited from Object |
- 实现
using System; using System.Collections; public class SamplesStack { public static void Main() { // Creates and initializes a new Stack. Stack myStack = new Stack(); myStack.Push("Hello"); myStack.Push("World"); myStack.Push("!"); // Displays the properties and values of the Stack. Console.WriteLine( "myStack" ); Console.WriteLine( "\tCount: {0}", myStack.Count ); Console.Write( "\tValues:" ); PrintValues( myStack ); } public static void PrintValues( IEnumerable myCollection ) { foreach ( Object obj in myCollection ) Console.Write( " {0}", obj ); Console.WriteLine(); } }