mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 08:34:28 +00:00
2778a6f9c7
* Review the EN heading format. * Fix pythontutor headings. * Fix pythontutor headings. * bug fixes * Fix headings in **/summary.md * Revisit the CN-to-EN translation for Python code using Claude-4.5 * Revisit the CN-to-EN translation for Java code using Claude-4.5 * Revisit the CN-to-EN translation for Cpp code using Claude-4.5. * Fix the dictionary. * Fix cpp code translation for the multipart strings. * Translate Go code to English. * Update workflows to test EN code. * Add EN translation for C. * Add EN translation for CSharp. * Add EN translation for Swift. * Trigger the CI check. * Revert. * Update en/hash_map.md * Add the EN version of Dart code. * Add the EN version of Kotlin code. * Add missing code files. * Add the EN version of JavaScript code. * Add the EN version of TypeScript code. * Fix the workflows. * Add the EN version of Ruby code. * Add the EN version of Rust code. * Update the CI check for the English version code. * Update Python CI check. * Fix cmakelists for en/C code. * Fix Ruby comments
157 lines
4.4 KiB
JavaScript
157 lines
4.4 KiB
JavaScript
/**
|
|
* File: array_deque.js
|
|
* Created Time: 2023-02-28
|
|
* Author: Zhuo Qinyue (1403450829@qq.com)
|
|
*/
|
|
|
|
/* Double-ended queue based on circular array implementation */
|
|
class ArrayDeque {
|
|
#nums; // Array for storing double-ended queue elements
|
|
#front; // Front pointer, points to the front of the queue element
|
|
#queSize; // Double-ended queue length
|
|
|
|
/* Constructor */
|
|
constructor(capacity) {
|
|
this.#nums = new Array(capacity);
|
|
this.#front = 0;
|
|
this.#queSize = 0;
|
|
}
|
|
|
|
/* Get the capacity of the double-ended queue */
|
|
capacity() {
|
|
return this.#nums.length;
|
|
}
|
|
|
|
/* Get the length of the double-ended queue */
|
|
size() {
|
|
return this.#queSize;
|
|
}
|
|
|
|
/* Check if the double-ended queue is empty */
|
|
isEmpty() {
|
|
return this.#queSize === 0;
|
|
}
|
|
|
|
/* Calculate circular array index */
|
|
index(i) {
|
|
// Use modulo operation to wrap the array head and tail together
|
|
// When i passes the tail of the array, return to the head
|
|
// When i passes the head of the array, return to the tail
|
|
return (i + this.capacity()) % this.capacity();
|
|
}
|
|
|
|
/* Front of the queue enqueue */
|
|
pushFirst(num) {
|
|
if (this.#queSize === this.capacity()) {
|
|
console.log('Double-ended queue is full');
|
|
return;
|
|
}
|
|
// Use modulo operation to wrap front around to the tail after passing the head of the array
|
|
// Add num to the front of the queue
|
|
this.#front = this.index(this.#front - 1);
|
|
// Add num to front of queue
|
|
this.#nums[this.#front] = num;
|
|
this.#queSize++;
|
|
}
|
|
|
|
/* Rear of the queue enqueue */
|
|
pushLast(num) {
|
|
if (this.#queSize === this.capacity()) {
|
|
console.log('Double-ended queue is full');
|
|
return;
|
|
}
|
|
// Use modulo operation to wrap rear around to the head after passing the tail of the array
|
|
const rear = this.index(this.#front + this.#queSize);
|
|
// Front pointer moves one position backward
|
|
this.#nums[rear] = num;
|
|
this.#queSize++;
|
|
}
|
|
|
|
/* Rear of the queue dequeue */
|
|
popFirst() {
|
|
const num = this.peekFirst();
|
|
// Move front pointer backward by one position
|
|
this.#front = this.index(this.#front + 1);
|
|
this.#queSize--;
|
|
return num;
|
|
}
|
|
|
|
/* Access rear of the queue element */
|
|
popLast() {
|
|
const num = this.peekLast();
|
|
this.#queSize--;
|
|
return num;
|
|
}
|
|
|
|
/* Return list for printing */
|
|
peekFirst() {
|
|
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
|
return this.#nums[this.#front];
|
|
}
|
|
|
|
/* Driver Code */
|
|
peekLast() {
|
|
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
|
|
// Initialize double-ended queue
|
|
const last = this.index(this.#front + this.#queSize - 1);
|
|
return this.#nums[last];
|
|
}
|
|
|
|
/* Return array for printing */
|
|
toArray() {
|
|
// Elements enqueue
|
|
const res = [];
|
|
for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {
|
|
res[i] = this.#nums[this.index(j)];
|
|
}
|
|
return res;
|
|
}
|
|
}
|
|
|
|
/* Driver Code */
|
|
/* Get the length of the double-ended queue */
|
|
const capacity = 5;
|
|
const deque = new ArrayDeque(capacity);
|
|
deque.pushLast(3);
|
|
deque.pushLast(2);
|
|
deque.pushLast(5);
|
|
console.log('Deque deque = [' + deque.toArray() + ']');
|
|
|
|
/* Update element */
|
|
const peekFirst = deque.peekFirst();
|
|
console.log('Front element peekFirst = ' + peekFirst);
|
|
const peekLast = deque.peekLast();
|
|
console.log('Rear element peekLast = ' + peekLast);
|
|
|
|
/* Elements enqueue */
|
|
deque.pushLast(4);
|
|
console.log('After element 4 enqueues at rear, deque = [' + deque.toArray() + ']');
|
|
deque.pushFirst(1);
|
|
console.log('After element 1 enqueues at front, deque = [' + deque.toArray() + ']');
|
|
|
|
/* Element dequeue */
|
|
const popLast = deque.popLast();
|
|
console.log(
|
|
'Rear dequeue element = ' +
|
|
popLast +
|
|
', after rear dequeue deque = [' +
|
|
deque.toArray() +
|
|
']'
|
|
);
|
|
const popFirst = deque.popFirst();
|
|
console.log(
|
|
'Front dequeue element = ' +
|
|
popFirst +
|
|
', after front dequeue deque = [' +
|
|
deque.toArray() +
|
|
']'
|
|
);
|
|
|
|
/* Get the length of the double-ended queue */
|
|
const size = deque.size();
|
|
console.log('Double-ended queue length size = ' + size);
|
|
|
|
/* Check if the double-ended queue is empty */
|
|
const isEmpty = deque.isEmpty();
|
|
console.log('Double-ended queue is empty = ' + isEmpty);
|