-
Notifications
You must be signed in to change notification settings - Fork 12
/
solution.java
130 lines (121 loc) · 3.83 KB
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
class MyCircularDeque {
// Array to hold the elements of the deque
private int[] deque;
// Pointers to the front and rear of the deque, and size to manage capacity
private int front, rear, size;
/**
* Constructor to initialize the deque with a given capacity.
*
* @param k The maximum number of elements the deque can hold.
*/
public MyCircularDeque(int k) {
// We allocate size k+1 to manage the circular nature (1 extra space for
// distinction between full and empty)
deque = new int[k + 1];
front = 0; // Front pointer starts at the 0th position
rear = 0; // Rear pointer starts at the 0th position
size = k + 1; // Total size is k+1 to differentiate between full and empty
}
/**
* Inserts an element at the front of the deque.
*
* @param value The value to be inserted.
* @return true if insertion is successful, false if deque is full.
*/
public boolean insertFront(int value) {
// Check if deque is full
if (isFull())
return false;
// Move the front pointer backward in a circular manner
front = (front - 1 + size) % size;
// Place the value at the new front position
deque[front] = value;
return true;
}
/**
* Inserts an element at the rear of the deque.
*
* @param value The value to be inserted.
* @return true if insertion is successful, false if deque is full.
*/
public boolean insertLast(int value) {
// Check if deque is full
if (isFull())
return false;
// Insert the value at the current rear position
deque[rear] = value;
// Move the rear pointer forward in a circular manner
rear = (rear + 1) % size;
return true;
}
/**
* Deletes an element from the front of the deque.
*
* @return true if deletion is successful, false if deque is empty.
*/
public boolean deleteFront() {
// Check if deque is empty
if (isEmpty())
return false;
// Move the front pointer forward in a circular manner
front = (front + 1) % size;
return true;
}
/**
* Deletes an element from the rear of the deque.
*
* @return true if deletion is successful, false if deque is empty.
*/
public boolean deleteLast() {
// Check if deque is empty
if (isEmpty())
return false;
// Move the rear pointer backward in a circular manner
rear = (rear - 1 + size) % size;
return true;
}
/**
* Gets the front element of the deque.
*
* @return The front element or -1 if the deque is empty.
*/
public int getFront() {
// Check if deque is empty
if (isEmpty())
return -1;
// Return the element at the front pointer
return deque[front];
}
/**
* Gets the rear element of the deque.
*
* @return The rear element or -1 if the deque is empty.
*/
public int getRear() {
// Check if deque is empty
if (isEmpty())
return -1;
// Return the element just before the rear pointer (rear - 1) in a circular
// manner
return deque[(rear - 1 + size) % size];
}
/**
* Checks if the deque is empty.
*
* @return true if deque is empty, false otherwise.
*/
public boolean isEmpty() {
// Deque is empty when the front and rear pointers are equal
return front == rear;
}
/**
* Checks if the deque is full.
*
* @return true if deque is full, false otherwise.
*/
public boolean isFull() {
// Deque is full when moving the rear pointer forward brings it to the front
// position
return (rear + 1) % size == front;
}
}