自己定義的棧的接口,完全是按照棧的常用方法以及命名方式實(shí)現(xiàn):
注意以下類,接口都是在一個命名空間下
棧的接口:包括了常用的方法
namespace 棧{ interface IStackDS<T> { int Count { get; } int GetLength(); bool IsEmpty(); void Clear(); void Push(T item); T Pop(); T Peek(); }}
順序棧的實(shí)現(xiàn),參照順序表實(shí)現(xiàn)
namespace 棧{ class SeqStack<T> : IStackDS<T> { private T[] data; //棧 private int top; //棧頂 //構(gòu)造函數(shù) public SeqStack(int size) { data = new T[size]; top = -1; } public SeqStack() : this(10) { data = new T[10]; top = -1; } //棧的元素個數(shù)屬性 public int Count { get { return top + 1; } } //清空棧的方法 public void Clear() { top = -1; } //獲取棧的長度的方法 public int GetLength() { return Count; } //查看棧頂元素是什么 public T Peek() { if (top == -1) { Console.WriteLine("當(dāng)前棧沒有元素"); return default(T); } return data[top]; } //出棧(取出棧頂元素,并移除) public T Pop() { T temp = data[top]; top--; return temp; } //入棧 public void Push(T item) { data[top + 1] = item; top++; } //棧中是否源元素 public bool IsEmpty() { return Count==0; } }}
以上是順序棧的實(shí)現(xiàn)
下面是鏈棧的實(shí)現(xiàn),定義一個節(jié)點(diǎn)類
namespace 棧{ /// <summary> /// 鏈棧的節(jié)點(diǎn) /// </summary> /// <typeparam name="T"></typeparam> class Node<T> { private T data; //數(shù)據(jù) private Node<T> next; //指向下一個的引用 //四個構(gòu)造函數(shù),不是都有用,,以及上面兩個變量的屬性, public Node() { data = default(T); next = null; } public Node(T data) { this.data = data; next = null; } public Node(T data , Node<T> index) { this.data = data; this.next = index; } public Node(Node<T> index) { data = default(T); next = index; } public T Data { get { return data; } set { data = value; } } public Node<T> Next { get { return next; } set { next = value; } } }}
鏈棧的實(shí)現(xiàn):
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 棧{ class LinkStark<T> : IStackDS<T> { private Node<T> top; //棧頂元素節(jié)點(diǎn) private int count = 0; //棧里面的元素 public int Count { get { return count; } } /// <summary> /// 清空棧中數(shù)據(jù) /// </summary> public void Clear() { count = 0; top = null; } /// <summary> /// 棧的長度 /// </summary> /// <returns></returns> public int GetLength() { return Count; } /// <summary> /// 棧中是否有數(shù)據(jù) /// </summary> /// <returns></returns> public bool IsEmpty() { return count == 0; } public T Peek() { return top.Data; } /// <summary> /// 出棧 取得棧頂元素然后刪除 /// </summary> /// <returns></returns> public T Pop() { T data = top.Data; top = top.Next; count--; return data; } /// <summary> /// 入棧 /// </summary> /// <param name="item"></param> public void Push(T item) {//把新添加的元素作為頭結(jié)點(diǎn),就是棧頂 Node<T> newNode = new Node<T>(item); newNode.Next = top; top = newNode; count++; } }}
以上是鏈棧的實(shí)現(xiàn)
下面是測試:
amespace 棧{ class Program { static void Main(string[] args) { //使用BCL中的棧 //Stack<char> stack = new Stack<char>(); //使用自己的棧 // SeqStack<char> stack = new SeqStack<char>(); //使用自己的鏈棧 IStackDS<char> stack = new LinkStark<char>(); stack.Push('a'); stack.Push('b'); stack.Push('c'); Console.WriteLine("push后的數(shù)據(jù)個數(shù)"+ stack.Count); char temp = stack.Pop(); Console.WriteLine("pop 之后數(shù)據(jù)是:"+ temp); Console.WriteLine("pop 之后數(shù)據(jù)個數(shù)"+stack.Count); char temp2 = stack.Peek(); Console.WriteLine("Peek 之后數(shù)據(jù)是:" + temp2); Console.WriteLine("Peek 之后數(shù)據(jù)個數(shù)" + stack.Count); Console.ReadKey(); } }}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對VEVB武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選