MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / MyCircularDeque

Class MyCircularDeque

Java/design-circular-deque.java:5–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3
4
5public class MyCircularDeque {
6
7
8 int size; // keeping the count of elements in your array
9 int front = -1; // keeping the index of front
10 int rear = -1; // keeping the index of rear
11 int capacity; // total capacity of our DS
12 int arr[]; // array to store the elements
13
14
15 /** Initialize your data structure here. Set the size of the deque to be k. */
16 public MyCircularDeque(int k) { // initialising capacity as k
17 this.size = 0 ; // and initialising the array
18 this.capacity = k;
19 arr = new int[k];
20 }
21
22 /** Adds an item at the front of Deque. Return true if the operation is successful. */
23 public boolean insertFront(int value) {
24 if( (this.size == this.capacity)) // means queue is full
25 return false;
26
27 if(front == -1) { // means queue is empty
28 arr[0] = value;
29 front = 0;
30 rear = 0;
31 this.size ++; // increase the size and return true
32 return true;
33 }
34
35 if(front == 0) { // if front is at 0, then add at last and make front as last
36 arr[this.capacity - 1] = value;
37 front = this.capacity - 1;
38 }else{ // else add to the left of front and make front as front - 1;
39 arr[front - 1] = value;
40 front --;
41 }
42 this.size ++; // increase the size and return true..
43 return true;
44 }
45
46 /** Adds an item at the rear of Deque. Return true if the operation is successful. */
47 public boolean insertLast(int value) {
48
49 if(this.size == this.capacity) // means queue is full
50 return false;
51
52 if(rear == -1) { // means queue is empty
53 arr[0] = value; // add the first element and return true
54 front = 0;
55 rear = 0;
56 this.size ++;
57 return true;
58 }
59
60 if(rear == this.capacity - 1) { // if rear is at last, means we will have to add this element at first
61 arr[0] = value; // position and make rear as first
62 rear = 0;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected