___19__C#手动编写Stack类,模拟栈的数据操作

来源:互联网 发布:工作流管理系统源码 编辑:程序博客网 时间:2024/06/06 08:54

Note节点类

// 节点类  class Node  {    // 节点有两个属性:自己、指向下一个节点    public Node Next;    public object Value;    public Node(object value) : this(value, null) {}    //创建对象的时候先调用Node(object value,Node next) ,则 Node next 这里为空    public Node(object value, Node next)    {      Next = next;      Value = value;    }  }




Stack类


 public class Stack  {    // first: 栈最上面一个节点    private Node first = null;        // count: 栈中节点的数量    private int count = 0;    // 判空属性,提供get访问器    public bool Empty    {      get      {        return (first == null);      }    }    // 计数属性,提供get访问器    public int Count    {       get       {          return count;       }     }  // 压栈操作,注意返回object  public object Pop()  {    if (first == null)    {      throw new InvalidOperationException ("Cant 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++;  }}



主程序 



static void Main(){    Stack s = new Stack();    if (s.Empty)      Console.WriteLine("堆栈为空");    else      Console.WriteLine("堆栈非空");    // 往栈中压入5个节点    for (int i = 0; i < 5; i++)      s.Push(i);    Console.WriteLine("往堆栈中压入了{0}个元素", s.Count);        // 把栈中节点全部弹出来    for (int i = 0; i < 5; i++)      Console.WriteLine("弹出了第{0}个元素,还剩{1}个元素。", (int)s.Pop()+1, s.Count);    s = null;}




0 0
原创粉丝点击