其他分享
首页 > 其他分享> > Design Circular Deque

Design Circular Deque

作者:互联网

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

Your implementation should support following operations:

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 }

 

标签:Deque,return,Design,front,size,true,rear,Circular
来源: https://www.cnblogs.com/beiyeqingteng/p/11333975.html