Design Circular Deque

Design your implementation of the circular double-ended queue (deque).

Your implementation should support following operations:

  • MyCircularDeque(k): Constructor, set the size of the deque to be k.
  • insertFront(): Adds an item at the front of Deque. Return true if the operation is successful.
  • insertLast(): Adds an item at the rear of Deque. Return true if the operation is successful.
  • deleteFront(): Deletes an item from the front of Deque. Return true if the operation is successful.
  • deleteLast(): Deletes an item from the rear of Deque. Return true if the operation is successful.
  • getFront(): Gets the front item from the Deque. If the deque is empty, return -1.
  • getRear(): Gets the last item from Deque. If the deque is empty, return -1.
  • isEmpty(): Checks whether Deque is empty or not.
  • isFull(): Checks whether Deque is full or not.

Example:

MyCircularDeque circularDeque = new MycircularDeque(3); // set the size to be 3
circularDeque.insertLast(1);			// return true
circularDeque.insertLast(2);			// return true
circularDeque.insertFront(3);			// return true
circularDeque.insertFront(4);			// return false, the queue is full
circularDeque.getRear();  			// return 2
circularDeque.isFull();				// return true
circularDeque.deleteLast();			// return true
circularDeque.insertFront(4);			// return true
circularDeque.getFront();			// return 4
 1 class MyCircularDeque {
 2     int[] a;
 3     int front, rear, cap, size;
 4
 5     public MyCircularDeque(int k) {
 6         a = new int[k];
 7         cap = k;
 8     }
 9
10     /** Adds an item at the front of Deque. Return true if the operation is successful. */
11     public boolean insertFront(int value) {
12         if(isFull()) return false;
13         // because we set the initial value of front to be 0
14         if (size != 0) {
15             front = (front - 1 + cap) % cap;
16         }
17         a[front] = value;
18         size++;
19         return true;
20     }
21
22     /** Adds an item at the rear of Deque. Return true if the operation is successful. */
23     public boolean insertLast(int value) {
24         if(isFull()) return false;
25         // because we set the initial value of rear to be 0
26         if (size != 0) {
27             rear = (rear + 1) % cap;
28         }
29         a[rear] = value;
30         size++;
31         return true;
32     }
33
34     /** Deletes an item from the front of Deque. Return true if the operation is successful. */
35     public boolean deleteFront() {
36         if(isEmpty()) return false;
37         size--;
38         if (size != 0) {
39             front = (front + 1) % cap;
40         }
41         return true;
42     }
43
44     /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
45     public boolean deleteLast() {
46         if(isEmpty()) return false;
47         size--;
48         if (size != 0) {
49             rear = (rear - 1 + cap) % cap;
50         }
51         return true;
52     }
53
54     /** Get the front item from the deque. */
55     public int getFront() {
56         return isEmpty() ? -1 : a[front];
57     }
58
59     /** Get the last item from the deque. */
60     public int getRear() {
61         return isEmpty() ? -1 : a[rear];
62     }
63
64     /** Checks whether the circular deque is empty or not. */
65     public boolean isEmpty() {
66         return size == 0;
67     }
68
69     /** Checks whether the circular deque is full or not. */
70     public boolean isFull() {
71          return size == cap;
72     }
73 }

原文地址:https://www.cnblogs.com/beiyeqingteng/p/11333975.html

时间: 2024-10-10 05:38:35

Design Circular Deque的相关文章

LeetCode 641. Design Circular Deque

原题链接在这里:https://leetcode.com/problems/design-circular-deque/ 题目: Design your implementation of the circular double-ended queue (deque). Your implementation should support following operations: MyCircularDeque(k): Constructor, set the size of the dequ

LeetCode 622. Design Circular Queue

原题链接在这里:https://leetcode.com/problems/design-circular-queue/ 题目: Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and th

[LeetCode] Design Circular Queue 设计环形队列

Design your implementation of the circular queue. The circular queue is a linear data structure in which the operations are performed based on FIFO (First In First Out) principle and the last position is connected back to the first position to make a

【LeetCode】设计题 design(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [146]LRU Cache [155]Min Stack [170]Two Sum III - Data structure design [173]Binary Search Tree Iterator [208]Implement Trie (Prefix Tree) [211]Add and Search Word - Data structure desig

【LeetCode】队列 queue(共8题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [346]Moving Average from Data Stream [353]Design Snake Game [363]Max Sum of Rectangle No Larger Than K [582]Kill Process [621]Task Scheduler [622]Design Circular Queue [641]Design Circu

设计循环队列——写起来最清爽的还使用原生的deque 双端队列

622. 设计循环队列 难度中等89收藏分享切换为英文关注反馈 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为“环形缓冲器”. 循环队列的一个好处是我们可以利用这个队列之前用过的空间.在一个普通队列里,一旦一个队列满了,我们就不能插入下一个元素,即使在队列前面仍有空间.但是使用循环队列,我们能使用这些空间去存储新的值. 你的实现应该支持如下操作: MyCircularQueue(k): 构造器,设置队

数据结构与算法:栈+队列+递归

[栈] Python实现: 1. 用数组实现一个顺序栈 class SStack(): def __init__(self): self._elems = [] def is_empty(self): return self._elems == [] def top(self): if self._elems == []: raise StackUnderflow("in SStack.top()") return self._elems[-1] def push(self, elem

python deque的内在实现 本质上就是双向链表所以用于stack、队列非常方便

How collections.deque works? Cosven 前言:在 Python 生态中,我们经常使用 collections.deque 来实现栈.队列这些只需要进行头尾操作的数据结构,它的 append/pop 操作都是 O(1) 时间复杂度.list 的 pop(0) 的时间复杂度是 O(n), 在这个场景中,它的效率没有 deque 高.那 deque 内部是怎样实现的呢? 我从 GitHub 上挖出了 CPython collections 模块的第二个 commit 的

POJ2100 Graveyard Design(尺取法)

POJ2100 Graveyard Design 题目大意:给定一个数n,求出一段连续的正整数的平方和等于n的方案数,并输出这些方案,注意输出格式: 循环判断条件可以适当剪支,提高效率,(1^2+2^2+..n^2)=n*(n+1)*(2n+1)/6; 尺取时一定要注意循环终止条件的判断. #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <