設計循環雙端隊列C#

設計實現雙端隊列。
你的實現需要支持以下操作:

MyCircularDeque(k):構造函數,雙端隊列的大小爲k。
insertFront():將一個元素添加到雙端隊列頭部。 如果操作成功返回 true。
insertLast():將一個元素添加到雙端隊列尾部。如果操作成功返回 true。
deleteFront():從雙端隊列頭部刪除一個元素。 如果操作成功返回 true。
deleteLast():從雙端隊列尾部刪除一個元素。如果操作成功返回 true。
getFront():從雙端隊列頭部獲得一個元素。如果雙端隊列爲空,返回 -1。
getRear():獲得雙端隊列的最後一個元素。 如果雙端隊列爲空,返回 -1。
isEmpty():檢查雙端隊列是否爲空。
isFull():檢查雙端隊列是否滿了。

示例:

MyCircularDeque circularDeque = new MycircularDeque(3); // 設置容量大小爲3
circularDeque.insertLast(1); // 返回 true
circularDeque.insertLast(2); // 返回 true
circularDeque.insertFront(3); // 返回 true
circularDeque.insertFront(4); // 已經滿了,返回 false
circularDeque.getRear(); // 返回 2
circularDeque.isFull(); // 返回 true
circularDeque.deleteLast(); // 返回 true
circularDeque.insertFront(4); // 返回 true
circularDeque.getFront(); // 返回 4

提示:

所有值的範圍爲 [1, 1000]
操作次數的範圍爲 [1, 1000]
請不要使用內置的雙端隊列庫。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/design-circular-deque
著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。

public class MyCircularDeque
        {
            private int[] dataset;
            public int MaxSize{ get; }
            private int pFront;
            private int pRear;
            public int Length { get; private set; }
            /** Initialize your data structure here. Set the size of the deque to be k. */
            public MyCircularDeque(int k)
            {
                MaxSize = k;
                dataset = new int[MaxSize];
                pFront = 0;
                pRear = 0;
                Length = 0;
            }

            /** Adds an item at the front of Deque. Return true if the operation is successful. */
            public bool InsertFront(int value)
            {
                if (Length == MaxSize)
                    return false;
                pFront--;
                pFront = (pFront + MaxSize) % MaxSize;
                dataset[pFront] = value;
                Length++;
                return true;
            }

            /** Adds an item at the rear of Deque. Return true if the operation is successful. */
            public bool InsertLast(int value)
            {
                if (Length == MaxSize)
                    return false;
                dataset[pRear] = value;
                pRear = (pRear + 1) % MaxSize;
                Length++;
                return true;
            }

            /** Deletes an item from the front of Deque. Return true if the operation is successful. */
            public bool DeleteFront()
            {
                if (Length == 0)
                    return false;
                pFront = (pFront + 1) % MaxSize;
                Length--;
                return true;
            }

            /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
            public bool DeleteLast()
            {
                if (Length == 0)
                    return false;
                pRear--;
                pRear = (pRear + MaxSize) % MaxSize;
                Length--;
                return true;
            }

            /** Get the front item from the deque. */
            public int GetFront()
            {
                if (Length == 0)
                    return -1;
                return dataset[pFront];
            }

            /** Get the last item from the deque. */
            public int GetRear()
            {
                if (Length == 0)
                    return -1;
                return dataset[(pRear -1+ MaxSize) % MaxSize];
            }

            /** Checks whether the circular deque is empty or not. */
            public bool IsEmpty()
            {
                return Length == 0 ? true : false;
            }

            /** Checks whether the circular deque is full or not. */
            public bool IsFull()
            {
                return Length == MaxSize ? true : false;
            }
        }

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * bool param_1 = obj.InsertFront(value);
 * bool param_2 = obj.InsertLast(value);
 * bool param_3 = obj.DeleteFront();
 * bool param_4 = obj.DeleteLast();
 * int param_5 = obj.GetFront();
 * int param_6 = obj.GetRear();
 * bool param_7 = obj.IsEmpty();
 * bool param_8 = obj.IsFull();
 */

模仿課上循環隊列的實現方式,設計兩個標記頭部和尾部的int,起指針的作用。
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章