using System;
using System.Collections.Generic;
namespace CSharp栈
{
class Program
{
static void Main(string[] args)
{
Stack<string> stack = new Stack<string>();
stack.Push("元素1");
stack.Push("元素2");
stack.Push("元素3");
stack.Push("元素4");
// 遍历元素
foreach (string item in stack)
{
Console.WriteLine(item);
}
//pop弹出元素,移除并返回位于 Stack 顶部的对象。
Console.WriteLine("
移除并返回位于 Stack 顶部的对象---{0}", stack.Pop());
//peek返回位于 Stack 顶部的对象但不将其移除。
Console.WriteLine("返回位于 Stack 顶部的对象但不将其移除---{0}", stack.Peek());
foreach (string item in stack)
{
Console.WriteLine(item);
}
// 创建新栈,复制元素
Stack<string> stackNew = new Stack<string>(stack.ToArray());
Console.WriteLine("
创建新栈,复制元素:");
foreach (string item in stackNew)
{
Console.WriteLine(item);
}
//contains用法
Console.WriteLine("
stackNew.Contains("元素3") = {0}",
stackNew.Contains("four"));
Console.WriteLine("
stackNew.Clear()");
stackNew.Clear();
Console.WriteLine("
stackNew.Count = {0}", stackNew.Count);
Console.ReadKey();
}
}
}