栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

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

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

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

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

先抽象堆栈的接口IStack:

namespace 栈与队列
{
  public interface IStack
  {
    /// 
    /// 返回堆栈的实际元素个数
    /// 
    /// 
    int Count();
    /// 
    /// 判断堆栈是否为空
    /// 
    /// 
    bool IsEmpty();
    /// 
    /// 清空堆栈里的元素
    /// 
    void Clear();
    /// 
    /// 入栈:将元素压入堆栈中
    /// 
    /// 
    void Push(T item);
    /// 
    /// 出栈:从堆栈顶取一个元素,并从堆栈中删除
    /// 
    /// 
    T Pop();
    /// 
    /// 取堆栈顶部的元素(但不删除)
    /// 
    /// 
    T Peek();
  }
}

顺序堆栈(SeqStack)的实现:

using System;
using System.Text;
namespace 栈与队列
{
  public class SeqStack:IStack
  {
    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
  {
    private T data;
    private Node next;
    public Node(T data, Node next) 
    {
      this.data = data;
      this.next = next;
    }
    public Node(Node 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 Next 
    {
      get { return next; }
      set { next = value; }
    }
  }
}

下面是linkStack.cs

using System;
using System.Text;
namespace 栈与队列
{
  public class linkStack:IStack
  {
    private Node top;
    private int num;//节点个数
    /// 
    /// 顶部节点
    /// 
    public Node 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 q = new Node(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 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 p = top;
 while (p.Next != null)
 {   
   sb.Append(p.Next.Data.ToString()+ ",");
   p = p.Next;
 }
      }
      return sb.ToString();
    }
  }
}

测试代码片段:

Console.WriteLine("顺序堆栈测试开始...");
SeqStack seqStack = new SeqStack(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 linkStack = new linkStack();
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已经提供了堆栈的基本实现,明白原理后,仍然推荐大家使用内置的实现。

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124575.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号