您的位置:首页 > 编程语言 > C#

C# 堆栈(Stack)

2015-05-06 20:49 330 查看
堆栈(Stack)代表了一个后进先出的对象集合。



using System;
using System.Collections;

namespace CollectionsApplication
{
class Program
{
static void Main(string[] args)
{
Stack st = new Stack();

st.Push('A');
st.Push('M');
st.Push('G');
st.Push('W');

Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
Console.WriteLine();

st.Push('V');
st.Push('H');
Console.WriteLine("The next poppable value in stack: {0}",
st.Peek());
Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
Console.WriteLine();

Console.WriteLine("Removing values ");
st.Pop();
st.Pop();
st.Pop();

Console.WriteLine("Current stack: ");
foreach (char c in st)
{
Console.Write(c + " ");
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: