欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#数据结构之堆栈(Stack)实例详解

程序员文章站 2022-06-25 08:58:08
本文实例讲述了c#数据结构之堆栈(stack)。分享给大家供大家参考,具体如下: 堆栈(stack)最明显的特征就是“先进后出”,本质上讲堆栈也是一种线性结构,符合线性结...

本文实例讲述了c#数据结构之堆栈(stack)。分享给大家供大家参考,具体如下:

堆栈(stack)最明显的特征就是“先进后出”,本质上讲堆栈也是一种线性结构,符合线性结构的基本特点:即每个节点有且只有一个前驱节点和一个后续节点。

相对前面学习过的、不同的地方在于:stack把所有操作限制在"只能在线性结构的某一端"进行,而不能在中间插入或删除元素。下面是示意图:

C#数据结构之堆栈(Stack)实例详解

从示意图中可以看出,堆栈有二种实现方式:基于数组的顺序堆栈实现、类似链表的链式堆栈实现

先抽象堆栈的接口istack:

namespace 栈与队列
{
  public interface istack<t>
  {
    /// <summary>
    /// 返回堆栈的实际元素个数
    /// </summary>
    /// <returns></returns>
    int count();
    /// <summary>
    /// 判断堆栈是否为空
    /// </summary>
    /// <returns></returns>
    bool isempty();
    /// <summary>
    /// 清空堆栈里的元素
    /// </summary>
    void clear();
    /// <summary>
    /// 入栈:将元素压入堆栈中
    /// </summary>
    /// <param name="item"></param>
    void push(t item);
    /// <summary>
    /// 出栈:从堆栈顶取一个元素,并从堆栈中删除
    /// </summary>
    /// <returns></returns>
    t pop();
    /// <summary>
    /// 取堆栈顶部的元素(但不删除)
    /// </summary>
    /// <returns></returns>
    t peek();
  }
}

顺序堆栈(seqstack)的实现:

using system;
using system.text;
namespace 栈与队列
{
  public class seqstack<t>:istack<t>
  {
    private int maxsize;
    private t[] data;
    private int top;    
    public seqstack(int size) 
    {
      data = new t[size];
      maxsize = size;
      top = -1;
    }
    #region //接口实现部分
    public int count() 
    {
      return top + 1;
    }
    public void clear() 
    {
      top = -1;
    }
    public bool isempty() 
    {
      return top == -1;
    }
    public void push(t item)
    {
      if (isfull())
      {
        console.writeline("stack is full");
        return;
      }
      data[++top] = item;
    }
    public t pop()
    {
      t tmp = default(t);
      if (isempty())
      {
        console.writeline("stack is empty");
        return tmp;
      }
      tmp = data[top];
      top--;
      return tmp;
    }
    public t peek()
    {
      if (isempty())
      {
        console.writeline("stack is empty!");
        return default(t);
      }
      return data[top];
    }
    #endregion
    public bool isfull() 
    {
      return top == maxsize - 1;
    }
    public override string tostring()
    {
      stringbuilder sb = new stringbuilder();
      for (int i = top;i>=0;i--)
      {
        sb.append(data[i] + ",");
      }
      return sb.tostring().trim(',');
    }    
  }
}

链式堆栈(linkstack)的实现

先定义节点node.cs

namespace 栈与队列
{
  public class node<t>
  {
    private t data;
    private node<t> next;
    public node(t data, node<t> next) 
    {
      this.data = data;
      this.next = next;
    }
    public node(node<t> next) 
    {
      this.next = next;
      this.data = default(t);
    }
    public node(t data) 
    {
      this.data = data;
      this.next = null;
    }
    public node() 
    {
      this.data = default(t);
      this.next = null;
    }
    public t data {
      get { return this.data; }
      set { this.data = value; }
    }
    public node<t> next 
    {
      get { return next; }
      set { next = value; }
    }
  }
}

下面是linkstack.cs

using system;
using system.text;
namespace 栈与队列
{
  public class linkstack<t>:istack<t>
  {
    private node<t> top;
    private int num;//节点个数
    /// <summary>
    /// 顶部节点
    /// </summary>
    public node<t> top 
    {
      get { return top; }
      set { top = value; }
    }
    public linkstack() 
    {
      top = null;
      num = 0;
    }
    public int count() 
    {
      return num;
    }
    public void clear() 
    {
      top = null;
      num = 0;
    }
    public bool isempty() 
    {
      if (top == null && num == 0)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    public void push(t item) 
    {
      node<t> q = new node<t>(item);
      if (top == null)
      {
        top = q;
      }
      else
      {
        q.next = top;
        top = q;
      }
      num++;
    }
    public t pop() 
    {
      if (isempty()) 
      {
        console.writeline("stack is empty!");
        return default(t);
      }
      node<t> p = top;
      top = top.next;
      num--;
      return p.data;
    }
    public t peek() 
    {
      if (isempty()) 
      {
        console.writeline("stack is empty!");
        return default(t);
      }
      return top.data;
    }
    public override string tostring()
    {
      stringbuilder sb = new stringbuilder();
      if (top != null) 
      {
        sb.append(top.data.tostring() + ",");
        node<t> p = top;
        while (p.next != null)
        {          
          sb.append(p.next.data.tostring()+ ",");
          p = p.next;
        }
      }
      return sb.tostring();
    }
  }
}

测试代码片段:

console.writeline("顺序堆栈测试开始...");
seqstack<int> seqstack = new seqstack<int>(10);
seqstack.push(1);
seqstack.push(2);
seqstack.push(3);
console.writeline(seqstack);
console.writeline(seqstack.peek());
console.writeline(seqstack);
console.writeline(seqstack.pop());
console.writeline(seqstack);
console.writeline("链堆栈测试开始...");
linkstack<int> linkstack = new linkstack<int>();
linkstack.push(1);
linkstack.push(2);
linkstack.push(3);
console.writeline(linkstack);
console.writeline(linkstack.peek());
console.writeline(linkstack);
console.writeline(linkstack.pop());
console.writeline(linkstack);
console.readline();

注: .net中system.collections.generic.stack<t>已经提供了堆栈的基本实现,明白原理后,仍然推荐大家使用内置的实现

希望本文所述对大家c#程序设计有所帮助。