1、一个简单实现栈的类
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace ConsoleApplication1 7 { 8 class Program 9 { 10 static void Main(string[] args) 11 { 12 Stack s = new Stack(); 13 s.Push(1); 14 s.Push(10); 15 s.Push(100); 16 Console.WriteLine(s.Pop()); 17 Console.ReadLine(); 18 Console.WriteLine(s.Pop()); 19 Console.ReadLine(); 20 Console.WriteLine(s.Pop()); 21 Console.ReadLine(); 22 23 } 24 } 25 26 public class Stack 27 { 28 Entry top; 29 public void Push(object data) 30 { 31 top = new Entry(top, data); //入栈 32 } 33 public object Pop() 34 { 35 if (top == null) throw new InvalidOperationException(); 36 object result = top.data; top = top.next; return result; //出栈 37 } 38 class Entry 39 { 40 public Entry next; 41 public object data; 42 public Entry(Entry next, object data) 43 { 44 this.next = next; //将当前值赋值给下一个 45 this.data = data; //新值赋值给当前值 46 } 47 } 48 } 49 50 }