1. 程式人生 > >C#實現順序棧

C#實現順序棧

1. BCL中順序棧

    BCL中有Stack,實現了棧的操作。

1.1 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _402_棧
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.使用BCL中的Stack<T>
            Stack<char> stack = new Stack<char>();
            stack.Push('2');
            stack.Push('6');
            stack.Push('f');
            stack.Push('h');

            Console.WriteLine("Push 後的個數" + stack.Count);

            Console.WriteLine("Pop之前的stack元素:");
            foreach (var temp in stack)
            {
                Console.Write(temp + " ");
            }
            char c = stack.Pop();//取得棧頂資料,並將該資料刪除
            Console.WriteLine("Pop之後得到的資料:" + c);
            Console.WriteLine("Pop之後的stack元素:");
            foreach (var temp in stack)
            {
                Console.Write(temp + " ");
            }

            char c2 = stack.Peek();//取得棧頂元素,不刪除
            Console.WriteLine("peek得到的資料:" + c2);
            Console.WriteLine("peek後棧中元素個數:" + stack.Count);

            stack.Clear();
            Console.WriteLine("Clear後棧中元素個數:" + stack.Count);
            //Console.WriteLine("空棧時取棧頂元素:" + stack.Peek());//為空棧時不能取棧頂元素,即不能進行peek pop操作,會出錯
            Console.ReadKey();
}
}
}

2. 自己實現順序棧

    定義一個順序棧的介面IStackDS,再實現介面中的方法。

2.1 IStackDS.cs(介面)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _402_棧
{
    interface IStackDS<T>
    {
        int Count { get; }//用來取得資料
        int GetLength();
        bool IsEmpty();
        void Clear();
        void Push(T item);
        T Peek();
        T Pop();
    }
}

2.2 SeqStack.cs(實現介面)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _402_棧
{
    class SeqStack<T> : IStackDS<T>
    {
        private T[] data;
        private int top;

        public SeqStack(int size)
        {
            data = new T[size];
            top = -1;
        }
        public SeqStack():this(10)
        {
            
        }
        public int Count {
            get
            {
                return top + 1;
            }
        }
        public void Clear()
        {
            top = -1;
        }

        public int GetLength()
        {
            return Count;
        }

        public bool IsEmpty()
        {
            return top == -1;
        }

        public T Peek()
        {
            return data[top];
        }

        public T Pop()
        {
            T temp = data[top];
            top--;
            return temp;
        }

        public void Push(T item)
        {
            data[top + 1] = item;
            top++;
        }
    }
}

2.3 Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _402_棧
{
    class Program
    {
        static void Main(string[] args)
        {
//2.使用自己定義的順序棧
            IStackDS<char> stack = new SeqStack<char>(30);
            stack.Push('2');
            stack.Push('6');
            stack.Push('f');
            stack.Push('h');

            Console.WriteLine("Push 後的個數" + stack.Count);

            
            char c = stack.Pop();//取得棧頂資料,並將該資料刪除
            Console.WriteLine("Pop之後得到的資料:" + c);

            char c2 = stack.Peek();//取得棧頂元素,不刪除
            Console.WriteLine("peek得到的資料:" + c2);
            Console.WriteLine("peek後棧中元素個數:" + stack.Count);

            stack.Clear();
            Console.WriteLine("Clear後棧中元素個數:" + stack.Count);
            Console.ReadKey();
        }
    }
}