資料結構C#版筆記–堆棧(Stack)

來源:互聯網
上載者:User

堆棧(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>已經提供了堆棧的基本實現,明白原理後,仍然推薦大家使用內建的實現。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.