程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#數據構造之客棧(Stack)實例詳解

C#數據構造之客棧(Stack)實例詳解

編輯:C#入門知識

C#數據構造之客棧(Stack)實例詳解。本站提示廣大學習愛好者:(C#數據構造之客棧(Stack)實例詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是C#數據構造之客棧(Stack)實例詳解正文


本文實例講述了C#數據構造之客棧(Stack)。分享給年夜家供年夜家參考,詳細以下:

客棧(Stack)最顯著的特點就是“先輩後出”,實質上講客棧也是一種線性構造,相符線性構造的根本特色:即每一個節點有且只要一個先驅節點和一個後續節點。

絕對後面進修過的次序表、鏈表分歧的處所在於: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#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved