码迷,mamicode.com
首页 > 其他好文 > 详细

Design Circular Deque

时间:2019-08-11 10:39:12      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:val   des   double   rtl   follow   ++   return   value   use   

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 }

 

Design Circular Deque

标签:val   des   double   rtl   follow   ++   return   value   use   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!