static void Main(string[] args) { //1,使用BCL中的Stack<T> //Stack<char> stack = new Stack<char>(); //2,使用我们自己的顺序栈 //IStackDS<char> stack = new SeqStack<char>(30); //3,使用我们自己的链栈 IStackDS <char> stack = new LinkStack <char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); //栈顶数据 Console.WriteLine("push a b c之后的数据个数为:" + stack.Count); char temp = stack.Pop(); //取得栈顶数据,并把栈顶的数据删除 Console.WriteLine("pop 之后得到的数据是:" + temp); Console.WriteLine("pop 之后栈中数据的个数:" + stack.Count); char temp2 = stack.Peek();//取得栈顶的数据,不删除 Console.WriteLine("peek 之后得到的数据是:" + temp2); Console.WriteLine("peek 之后栈中数据的个数:" + stack.Count); stack.Clear(); Console.WriteLine("clear 之后栈中数据的个数:" + stack.Count); //Console.WriteLine("空栈的时候,取得栈顶的值:"+stack.Peek());// 出现异常 //当空栈的时候,不要进行peek或者pop操作 ,否则会出现异常 Console.ReadKey(); }
static void Main(string[] args) { //Stack<char> stack = new Stack<char>(); //使用自定义的顺序栈: //IStackDS<char> stack = new SeqStack<char>(); //使用自定义的链栈: IStackDS <char> stack = new LinkStack <char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); Console.WriteLine(stack.Count); Console.WriteLine(stack.Pop()); //找到栈顶元素并删除 Console.WriteLine(stack.Count); Console.WriteLine(stack.Peek()); //找到栈顶元素但不删除 Console.WriteLine(stack.Count); stack.Clear(); Console.WriteLine(stack.Count); Console.ReadKey(); }
static void Main(string[] args) { //Stack<char> stack = new Stack<char>(); //IStackDS<char> stack = new SeqStack<char>(30); IStackDS <char> stack = new LinkStack <char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); Console.WriteLine(stack.Count); char temp = stack.Pop(); Console.WriteLine(temp); Console.WriteLine(stack.Count); char temp2 = stack.Peek(); Console.WriteLine(temp2); Console.WriteLine(stack.Count); stack.Clear(); Console.WriteLine(stack.Count); Console.ReadKey(); }