C# 資料結構與演算法系列(五) 隊列

來源:互聯網
上載者:User
隊列是一種特殊的線性表,它只允許在表的前端(front)進行刪除操作,而在表的後端(back)進行插入操作。進行插入操作的端稱為隊尾,進行刪除操作的端稱為隊頭。這也就是我們平常經常用說到的先進先出法則(FIFO),隊列這種法則,在中國好久以前就開始運用了,例如糧倉管理官員,在沒掌握這種法則前,倉庫底部的糧食都因時間太久而壞掉了,後來有聰明人士在糧倉二邊開個門,一邊進倉一邊出倉,這樣管理就方便多了。隊列中沒有元素時,稱為空白隊列。
隊列實現的介面如下:    public interface IQueen<T>
    {
        int Length();
        bool IsEmpty();
        bool IsFull();
        void Clear();
        void IN(T items);
        T Out();
        T GetFrontItem();
    }

隊列實現的原理與代碼如下:

Code
    public class JQueen<T> : IQueen<T>
    {
        private int size;
        private T[] item;
        private int front;
        private int back;

        public JQueen()
            : this(100)
        {
            size = 100;
            item = new T[100];
            front = back = -1;
        }

        public JQueen(int length)
        {
            size = length;
            item = new T[length];
            front = back = -1;
        }

        public T this[int index]
        {
            get { return item[index]; }
            set { item[index] = value; }
        }

        public int Front
        {
            get { return front; }
            set { front = value; }            
        }

        public int Back
        {
            get { return back; }
            set { back = value; }
        }

        public int MaxLength
        {
            get { return size; }
            set { size = value; }
        }        

        public int Length()
        {
            return (back - front + size) % size;
        }

        public bool IsEmpty()
        {
            return (front == back);
        }

        public bool IsFull()
        {
            return ((back + 1) % size == front);
        }

        public void Clear()
        {
            front = back = -1;
        }

        public void IN(T items)
        {
            if (IsFull())
            {
                throw new ArgumentOutOfRangeException("RangeException", "Queen RangeException: queen is full");
            }
            item[++back] = items;
        }

        public T Out()
        {
            T tmp = default(T);
            if (IsEmpty())
            {
                throw new ArgumentOutOfRangeException("RangeException", "Queen RangeException: queen is empty");
            }
            tmp = item[++front];
            return tmp;
        }

        public T GetFrontItem()
        {
            if (IsEmpty())
            {
                throw new ArgumentOutOfRangeException("RangeException", "Queen RangeException: queen is empty");
            }
            return item[front + 1];
        }

    }

測試隊列代碼:

   

Code
    public class Program
    {
        static void Main(string[] args)
        {
            try
            {
                JQueen<string> JQ = new JQueen<string>();
                Console.WriteLine(JQ.IsEmpty());  //是否為空白
                Console.WriteLine(JQ.IsFull());   //是否滿隊
                Console.WriteLine(JQ.MaxLength);  //初始化時隊列的長度
                Console.WriteLine(JQ.Length());     //隊列元素長度
                Console.WriteLine(JQ.Front);      //隊頭位置
                Console.WriteLine(JQ.Back);       //隊尾位置
                JQ.IN("A");  //插入元素
                JQ.IN("B");
                JQ.IN("C");
                JQ.IN("D");
                Console.WriteLine(JQ.GetFrontItem());   //隊頭元素
                Console.WriteLine("------元素出隊後隊頭元素-------");
                JQ.Out();  //出A
                JQ.Out(); 
                Console.WriteLine(JQ.GetFrontItem());   //出隊二個元素後隊頭元素
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);   //異常
                Console.ReadLine();
            }
        }
    }

結果如下:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.