用C#的类实现数据结构的堆栈算法

发表于:2007-06-17来源:作者:点击数: 标签:
using System; namespace DataStructure { /// summary> /// Class1 的摘要说明。 /// /summary> public class Stack//栈类 { private int count=0; private Node first=null;//定义首结点 public bool Empty { get { return(first==null); } } public int C

   
  using System;
  namespace DataStructure
  {
  /// <summary>
  /// Class1 的摘要说明。
  /// </summary>
  public class Stack//栈类
  {
  private int count=0;

  private Node first=null;//定义首结点
  public bool Empty
  {
  get
  {
  return(first==null);
  }
  }
  public int Count
  {
  get
  {
  return count;
  }
  }
  public object Pop()//入栈
  {
  if(first==null)
  {
  throw new InvalidOperationException("Can not pop from an empty stack;");
  }
  else
  {
  object temp=first.Value;
  first=first.Next;
  count--;
  return temp;
  }
  }
  public void push(object o)//出栈
  {
  first=new Node(o,first);
  count++;
  }
  public Stack()
  {
  //
  // TODO: 在此处添加构造函数逻辑
  //
  }
  }
  class Node //结点类
  {
  public Node Next;
  public object Value;
  public Node(object value):this(value,null){}
  public Node(object value,Node next)
  {
  Next=next;
  Value=value;
  }
  }
  }

原文转自:http://www.ltesting.net