软考辅导:数据结构(Stack栈)

来源:网络发布时间:2009-01-06
 class Node
  { int a;
  public Node(int a)
  { this.a=a; }
  public int A
  { get{return a;} set{a=value;} }
  public Node next;
  }
  class LinkedList
  { protected Node header;
  public void Generate(int x)
  { if(header==null)
  header=new Node(x);
  else
  { Node n = new Node(x);
  n.next=header;
  header=n;
  }
  }
  public void Out()
  { Node tmp=header;
  while(tmp!=null)
  { Console.WriteLine(tmp.A);
  tmp = tmp.next;
  } } }
  class Stack : LinkedList
  { public void Push(int x)
  { this.Generate(x); }
  public int Pop()
  { if(this.header == null)
  return -1; // empty stack
  int n = header.A;
  header = header.next;
  return n;
  }
  }
  class Test
  { static void Main()
  { Stack ss = new Stack();
  ss.Push(7);
  ss.Push(78);
  ss.Push(9);
  ss.Push(2);
  int i = ss.Pop();
  while(i != -1)
  { Console.WriteLine(i);
  i = ss.Pop();
  } } } }