Sponsored
Sponsored
This approach involves using a fixed-size array to represent the deque. We'll maintain two indices, front
and rear
, to manage the current front and last positions in the deque. Operations like insertions and deletions are performed by adjusting these indices while ensuring they wrap around using the modulo operation as necessary to remain within the array bounds.
Time Complexity: O(1) for each operation.
Space Complexity: O(k), where k is the capacity of the deque.
1class MyCircularDeque {
2 constructor(k) {
3 this.data = new Array(k).fill(-1);
4 this.front = 0;
5 this.rear = 0;
6 this.size = 0;
7 this.capacity = k;
8 }
9
10 insertFront(value) {
11 if (this.isFull()) return false;
12 this.front = (this.front - 1 + this.capacity) % this.capacity;
13 this.data[this.front] = value;
14 this.size++;
15 return true;
16 }
17
18 insertLast(value) {
19 if (this.isFull()) return false;
20 this.data[this.rear] = value;
21 this.rear = (this.rear + 1) % this.capacity;
22 this.size++;
23 return true;
24 }
25
26 deleteFront() {
27 if (this.isEmpty()) return false;
28 this.front = (this.front + 1) % this.capacity;
29 this.size--;
30 return true;
31 }
32
33 deleteLast() {
34 if (this.isEmpty()) return false;
35 this.rear = (this.rear - 1 + this.capacity) % this.capacity;
36 this.size--;
37 return true;
38 }
39
40 getFront() {
41 if (this.isEmpty()) return -1;
42 return this.data[this.front];
43 }
44
45 getRear() {
46 if (this.isEmpty()) return -1;
47 return this.data[(this.rear - 1 + this.capacity) % this.capacity];
48 }
49
50 isEmpty() {
51 return this.size === 0;
52 }
53
54 isFull() {
55 return this.size === this.capacity;
56 }
57}
In the JavaScript version, a fixed-size array with circular indexing is used. Insertions and deletions manipulate front
and rear
pointers using a wraparound logic suitable for the circular nature of the deque.
This approach makes use of a doubly linked list to implement the deque. This is particularly effective because it offers dynamic memory usage which can grow or shrink with the number of elements, instead of relying on a pre-allocated fixed-size structure as with arrays.
Time Complexity: O(1) for all operations.
Space Complexity: O(n), where n is the number of elements currently in the deque (potentially more efficient if n is much less than the initial capacity).
1 public int Value;
public Node Next;
public Node Prev;
public Node(int value) {
Value = value;
Next = Prev = null;
}
}
public class MyCircularDeque {
private Node front;
private Node rear;
private int size;
private int capacity;
public MyCircularDeque(int k) {
front = rear = null;
size = 0;
capacity = k;
}
public bool InsertFront(int value) {
if (IsFull()) return false;
Node node = new Node(value);
node.Next = front;
if (front != null) front.Prev = node;
front = node;
if (rear == null) rear = node;
size++;
return true;
}
public bool InsertLast(int value) {
if (IsFull()) return false;
Node node = new Node(value);
node.Prev = rear;
if (rear != null) rear.Next = node;
rear = node;
if (front == null) front = node;
size++;
return true;
}
public bool DeleteFront() {
if (IsEmpty()) return false;
front = front.Next;
if (front != null) front.Prev = null;
else rear = null;
size--;
return true;
}
public bool DeleteLast() {
if (IsEmpty()) return false;
rear = rear.Prev;
if (rear != null) rear.Next = null;
else front = null;
size--;
return true;
}
public int GetFront() {
return IsEmpty() ? -1 : front.Value;
}
public int GetRear() {
return IsEmpty() ? -1 : rear.Value;
}
public bool IsEmpty() {
return size == 0;
}
public bool IsFull() {
return size == capacity;
}
}
In C#, the linked list model for the deque supports dynamic sizing, letting each node connect bidirectionally, which allows rapid pointer modifications for diverse operations at both front and end.