Retranslate the complete English edition from Simplified Chinese (#2010)

* Retranslate the complete English edition from Simplified Chinese

* Polish three English explanations after reader-focused review
This commit is contained in:
Yudong Jin
2026-10-31 03:35:51 +08:00
committed by GitHub
parent ef46e2ed95
commit 14c314c08c
1502 changed files with 29459 additions and 23517 deletions

View File

@@ -4,11 +4,11 @@
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
/* Double-ended queue based on circular array implementation */
/* Deque implemented with a circular array */
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
#nums; // Array for storing deque elements
#front; // Front pointer, pointing to the first element
#queSize; // Deque size
/* Constructor */
constructor(capacity) {
@@ -17,89 +17,89 @@ class ArrayDeque {
this.#queSize = 0;
}
/* Get the capacity of the double-ended queue */
/* Get the deque capacity */
capacity() {
return this.#nums.length;
}
/* Get the length of the double-ended queue */
/* Get the deque size */
size() {
return this.#queSize;
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
isEmpty() {
return this.#queSize === 0;
}
/* Calculate circular array index */
/* Calculate an index in the circular array */
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
// Use modulo to connect the end of the array to its beginning
// When i passes the end of the array, wrap around to the beginning
// When i passes the beginning of the array, wrap around to the end
return (i + this.capacity()) % this.capacity();
}
/* Front of the queue enqueue */
/* Enqueue at the front */
pushFirst(num) {
if (this.#queSize === this.capacity()) {
console.log('Double-ended queue is full');
console.log('Deque 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
// Move the front pointer one position to the left
// Use modulo to wrap front to the end when it passes the beginning of the array
this.#front = this.index(this.#front - 1);
// Add num to front of queue
// Add num to the front
this.#nums[this.#front] = num;
this.#queSize++;
}
/* Rear of the queue enqueue */
/* Enqueue at the rear */
pushLast(num) {
if (this.#queSize === this.capacity()) {
console.log('Double-ended queue is full');
console.log('Deque is full');
return;
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Calculate the rear pointer, which points one position past the last element
const rear = this.index(this.#front + this.#queSize);
// Front pointer moves one position backward
// Add num to the rear
this.#nums[rear] = num;
this.#queSize++;
}
/* Rear of the queue dequeue */
/* Dequeue from the front */
popFirst() {
const num = this.peekFirst();
// Move front pointer backward by one position
// Advance the front pointer by one position
this.#front = this.index(this.#front + 1);
this.#queSize--;
return num;
}
/* Access rear of the queue element */
/* Dequeue from the rear */
popLast() {
const num = this.peekLast();
this.#queSize--;
return num;
}
/* Return list for printing */
/* Access the front element */
peekFirst() {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
if (this.isEmpty()) throw new Error('Deque is empty');
return this.#nums[this.#front];
}
/* Driver Code */
/* Access the rear element */
peekLast() {
if (this.isEmpty()) throw new Error('The Deque Is Empty.');
// Initialize double-ended queue
if (this.isEmpty()) throw new Error('Deque is empty');
// Calculate the index of the last element
const last = this.index(this.#front + this.#queSize - 1);
return this.#nums[last];
}
/* Return array for printing */
/* Return an array for printing */
toArray() {
// Elements enqueue
// Convert only the list elements within the occupied range
const res = [];
for (let i = 0, j = this.#front; i < this.#queSize; i++, j++) {
res[i] = this.#nums[this.index(j)];
@@ -108,8 +108,8 @@ class ArrayDeque {
}
}
/* Driver Code */
/* Get the length of the double-ended queue */
/* Driver code */
/* Initialize the deque */
const capacity = 5;
const deque = new ArrayDeque(capacity);
deque.pushLast(3);
@@ -117,40 +117,40 @@ deque.pushLast(2);
deque.pushLast(5);
console.log('Deque deque = [' + deque.toArray() + ']');
/* Update element */
/* Access an element */
const peekFirst = deque.peekFirst();
console.log('Front element peekFirst = ' + peekFirst);
const peekLast = deque.peekLast();
console.log('Rear element peekLast = ' + peekLast);
/* Elements enqueue */
/* Enqueue elements */
deque.pushLast(4);
console.log('After element 4 enqueues at rear, deque = [' + deque.toArray() + ']');
console.log('After enqueuing 4 at the rear, deque = [' + deque.toArray() + ']');
deque.pushFirst(1);
console.log('After element 1 enqueues at front, deque = [' + deque.toArray() + ']');
console.log('After enqueuing 1 at the front, deque = [' + deque.toArray() + ']');
/* Element dequeue */
/* Dequeue an element */
const popLast = deque.popLast();
console.log(
'Rear dequeue element = ' +
'Element dequeued from the rear = ' +
popLast +
', after rear dequeue deque = [' +
'; after removing from the rear, deque = [' +
deque.toArray() +
']'
);
const popFirst = deque.popFirst();
console.log(
'Front dequeue element = ' +
'Element dequeued from the front = ' +
popFirst +
', after front dequeue deque = [' +
'; after removing from the front, deque = [' +
deque.toArray() +
']'
);
/* Get the length of the double-ended queue */
/* Get the deque size */
const size = deque.size();
console.log('Double-ended queue length size = ' + size);
console.log('Deque size = ' + size);
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
const isEmpty = deque.isEmpty();
console.log('Double-ended queue is empty = ' + isEmpty);
console.log('Is the deque empty = ' + isEmpty);

View File

@@ -4,27 +4,27 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* Queue based on circular array implementation */
/* Queue implemented with a circular array */
class ArrayQueue {
#nums; // Array for storing queue elements
#front = 0; // Front pointer, points to the front of the queue element
#queSize = 0; // Queue length
#nums; // Array that stores the queue elements
#front = 0; // Front pointer, pointing to the first element
#queSize = 0; // Queue size
constructor(capacity) {
this.#nums = new Array(capacity);
}
/* Get the capacity of the queue */
/* Get the queue capacity */
get capacity() {
return this.#nums.length;
}
/* Get the length of the queue */
/* Get the queue size */
get size() {
return this.#queSize;
}
/* Check if the queue is empty */
/* Check whether the queue is empty */
isEmpty() {
return this.#queSize === 0;
}
@@ -35,10 +35,10 @@ class ArrayQueue {
console.log('Queue is full');
return;
}
// Use modulo operation to wrap rear around to the head after passing the tail of the array
// Add num to the rear of the queue
// Calculate the rear pointer, which points one position past the last element
// Use modulo to wrap rear to the beginning when it passes the end of the array
const rear = (this.#front + this.size) % this.capacity;
// Front pointer moves one position backward
// Add num to the rear
this.#nums[rear] = num;
this.#queSize++;
}
@@ -46,21 +46,21 @@ class ArrayQueue {
/* Dequeue */
pop() {
const num = this.peek();
// Move front pointer backward by one position, if it passes the tail, return to array head
// Advance the front pointer by one position, wrapping to the beginning if it passes the end
this.#front = (this.#front + 1) % this.capacity;
this.#queSize--;
return num;
}
/* Return list for printing */
/* Access the front element */
peek() {
if (this.isEmpty()) throw new Error('Queue is empty');
return this.#nums[this.#front];
}
/* Return Array */
/* Return an Array */
toArray() {
// Elements enqueue
// Convert only the list elements within the occupied range
const arr = new Array(this.size);
for (let i = 0, j = this.#front; i < this.size; i++, j++) {
arr[i] = this.#nums[j % this.capacity];
@@ -69,12 +69,12 @@ class ArrayQueue {
}
}
/* Driver Code */
/* Access front of the queue element */
/* Driver code */
/* Initialize the queue */
const capacity = 10;
const queue = new ArrayQueue(capacity);
/* Elements enqueue */
/* Enqueue elements */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -82,25 +82,25 @@ queue.push(5);
queue.push(4);
console.log('Queue queue =', queue.toArray());
/* Return list for printing */
/* Access the front element */
const peek = queue.peek();
console.log('Front element peek = ' + peek);
/* Element dequeue */
/* Dequeue an element */
const pop = queue.pop();
console.log('Dequeue element pop = ' + pop + ', after dequeue queue =', queue.toArray());
console.log('Dequeued element pop = ' + pop + '; after dequeuing, queue =', queue.toArray());
/* Get the length of the queue */
/* Get the queue size */
const size = queue.size;
console.log('Queue length size = ' + size);
console.log('Queue size = ' + size);
/* Check if the queue is empty */
/* Check whether the queue is empty */
const isEmpty = queue.isEmpty();
console.log('Queue is empty = ' + isEmpty);
console.log('Is the queue empty = ' + isEmpty);
/* Test circular array */
/* Test the circular array */
for (let i = 0; i < 10; i++) {
queue.push(i);
queue.pop();
console.log('Round ' + i + ' rounds of enqueue + dequeue, queue =', queue.toArray());
console.log('Round ' + i + ' : after enqueue + dequeue, queue =', queue.toArray());
}

View File

@@ -4,19 +4,19 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* Stack based on array implementation */
/* Stack implemented with an array */
class ArrayStack {
#stack;
constructor() {
this.#stack = [];
}
/* Get the length of the stack */
/* Get the stack size */
get size() {
return this.#stack.length;
}
/* Check if the stack is empty */
/* Check whether the stack is empty */
isEmpty() {
return this.#stack.length === 0;
}
@@ -32,23 +32,23 @@ class ArrayStack {
return this.#stack.pop();
}
/* Return list for printing */
/* Access the top element */
top() {
if (this.isEmpty()) throw new Error('Stack is empty');
return this.#stack[this.#stack.length - 1];
}
/* Return Array */
/* Return an Array */
toArray() {
return this.#stack;
}
}
/* Driver Code */
/* Access top of the stack element */
/* Driver code */
/* Initialize the stack */
const stack = new ArrayStack();
/* Elements push onto stack */
/* Push elements onto the stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -57,19 +57,19 @@ stack.push(4);
console.log('Stack stack = ');
console.log(stack.toArray());
/* Return list for printing */
/* Access the top element */
const top = stack.top();
console.log('Stack top element top = ' + top);
console.log('Top element top = ' + top);
/* Element pop from stack */
/* Pop an element from the stack */
const pop = stack.pop();
console.log('Pop element pop = ' + pop + ', after pop, stack = ');
console.log('Popped element pop = ' + pop + '; after popping, stack = ');
console.log(stack.toArray());
/* Get the length of the stack */
/* Get the stack size */
const size = stack.size;
console.log('Stack length size = ' + size);
console.log('Stack size = ' + size);
/* Check if empty */
/* Check whether it is empty */
const isEmpty = stack.isEmpty();
console.log('Stack is empty = ' + isEmpty);
console.log('Is the stack empty = ' + isEmpty);

View File

@@ -4,41 +4,41 @@
* Author: Zhuo Qinyue (1403450829@qq.com)
*/
/* Driver Code */
/* Get the length of the double-ended queue */
// JavaScript has no built-in deque, can only use Array as deque
/* Driver code */
/* Initialize the deque */
// JavaScript has no built-in deque; use an Array as a deque
const deque = [];
/* Elements enqueue */
/* Enqueue elements */
deque.push(2);
deque.push(5);
deque.push(4);
// Note: due to array, unshift() method has O(n) time complexity
// Note: because this uses an array, unshift() has O(n) time complexity
deque.unshift(3);
deque.unshift(1);
console.log('Double-ended queue deque = ', deque);
console.log('Deque deque = ', deque);
/* Update element */
/* Access an element */
const peekFirst = deque[0];
console.log('Front element peekFirst = ' + peekFirst);
const peekLast = deque[deque.length - 1];
console.log('Rear element peekLast = ' + peekLast);
/* Element dequeue */
// Note: due to array, shift() method has O(n) time complexity
/* Dequeue an element */
// Note: because this uses an array, shift() has O(n) time complexity
const popFront = deque.shift();
console.log(
'Front dequeue element popFront = ' + popFront + ', after front dequeue, deque = ' + deque
'Element removed from the front, popFront = ' + popFront + ', after dequeuing from the front, deque = ' + deque
);
const popBack = deque.pop();
console.log(
'Dequeue rear element popBack = ' + popBack + ', after rear dequeue, deque = ' + deque
'Element removed from the rear, popBack = ' + popBack + ', after dequeuing from the rear, deque = ' + deque
);
/* Get the length of the double-ended queue */
/* Get the deque size */
const size = deque.length;
console.log('Double-ended queue length size = ' + size);
console.log('Deque size = ' + size);
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
const isEmpty = size === 0;
console.log('Double-ended queue is empty = ' + isEmpty);
console.log('Is the deque empty = ' + isEmpty);

View File

@@ -6,8 +6,8 @@
/* Doubly linked list node */
class ListNode {
prev; // Predecessor node reference (pointer)
next; // Successor node reference (pointer)
prev; // Reference (pointer) to the previous node
next; // Reference (pointer) to the next node
val; // Node value
constructor(val) {
@@ -17,11 +17,11 @@ class ListNode {
}
}
/* Double-ended queue based on doubly linked list implementation */
/* Deque implemented with a doubly linked list */
class LinkedListDeque {
#front; // Head node front
#rear; // Tail node rear
#queSize; // Length of the double-ended queue
#queSize; // Deque size
constructor() {
this.#front = null;
@@ -29,7 +29,7 @@ class LinkedListDeque {
this.#queSize = 0;
}
/* Rear of the queue enqueue operation */
/* Enqueue at the rear */
pushLast(val) {
const node = new ListNode(val);
// If the linked list is empty, make both front and rear point to node
@@ -40,12 +40,12 @@ class LinkedListDeque {
// Add node to the tail of the linked list
this.#rear.next = node;
node.prev = this.#rear;
this.#rear = node; // Update tail node
this.#rear = node; // Update the tail node
}
this.#queSize++;
}
/* Front of the queue enqueue operation */
/* Enqueue at the front */
pushFirst(val) {
const node = new ListNode(val);
// If the linked list is empty, make both front and rear point to node
@@ -56,76 +56,76 @@ class LinkedListDeque {
// Add node to the head of the linked list
this.#front.prev = node;
node.next = this.#front;
this.#front = node; // Update head node
this.#front = node; // Update the head node
}
this.#queSize++;
}
/* Temporarily store tail node value */
/* Dequeue from the rear */
popLast() {
if (this.#queSize === 0) {
return null;
}
const value = this.#rear.val; // Store tail node value
// When the deque has one element, front and rear point to the same node
const value = this.#rear.val; // Store the tail node value
// When the queue size is 1, the head and tail point to the same node
if (this.#queSize === 1) {
this.#front = null;
this.#rear = null;
}
// Update tail node
// Remove the tail node
else {
let temp = this.#rear.prev;
temp.next = null;
this.#rear.prev = null;
this.#rear = temp; // Update tail node
this.#rear = temp; // Update the tail node
}
this.#queSize--;
return value;
}
/* Temporarily store head node value */
/* Dequeue from the front */
popFirst() {
if (this.#queSize === 0) {
return null;
}
const value = this.#front.val; // Store the head node value
// When the deque has one element, front and rear point to the same node
// When the queue size is 1, the head and tail point to the same node
if (this.#queSize === 1) {
this.#front = null;
this.#rear = null;
}
// Delete head node
// Remove the head node
else {
let temp = this.#front.next;
temp.prev = null;
this.#front.next = null;
this.#front = temp; // Update head node
this.#front = temp; // Update the head node
}
this.#queSize--;
return value;
}
/* Driver Code */
/* Access the rear element */
peekLast() {
return this.#queSize === 0 ? null : this.#rear.val;
}
/* Return list for printing */
/* Access the front element */
peekFirst() {
return this.#queSize === 0 ? null : this.#front.val;
}
/* Get the length of the double-ended queue */
/* Get the deque size */
size() {
return this.#queSize;
}
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
isEmpty() {
return this.#queSize === 0;
}
/* Print deque */
/* Print the deque */
print() {
const arr = [];
let temp = this.#front;
@@ -137,8 +137,8 @@ class LinkedListDeque {
}
}
/* Driver Code */
/* Get the length of the double-ended queue */
/* Driver code */
/* Initialize the deque */
const linkedListDeque = new LinkedListDeque();
linkedListDeque.pushLast(3);
linkedListDeque.pushLast(2);
@@ -146,32 +146,32 @@ linkedListDeque.pushLast(5);
console.log('Deque linkedListDeque = ');
linkedListDeque.print();
/* Update element */
/* Access an element */
const peekFirst = linkedListDeque.peekFirst();
console.log('Front element peekFirst = ' + peekFirst);
const peekLast = linkedListDeque.peekLast();
console.log('Rear element peekLast = ' + peekLast);
/* Elements enqueue */
/* Enqueue elements */
linkedListDeque.pushLast(4);
console.log('After element 4 enqueues at rear, linkedListDeque = ');
console.log('After enqueuing 4 at the rear, linkedListDeque = ');
linkedListDeque.print();
linkedListDeque.pushFirst(1);
console.log('After element 1 enqueues at front, linkedListDeque = ');
console.log('After enqueuing 1 at the front, linkedListDeque = ');
linkedListDeque.print();
/* Element dequeue */
/* Dequeue an element */
const popLast = linkedListDeque.popLast();
console.log('Rear dequeue element = ' + popLast + ', after rear dequeue linkedListDeque = ');
console.log('Element dequeued from the rear = ' + popLast + '; after removing from the rear, linkedListDeque = ');
linkedListDeque.print();
const popFirst = linkedListDeque.popFirst();
console.log('Front dequeue element = ' + popFirst + ', after front dequeue linkedListDeque = ');
console.log('Element dequeued from the front = ' + popFirst + '; after removing from the front, linkedListDeque = ');
linkedListDeque.print();
/* Get the length of the double-ended queue */
/* Get the deque size */
const size = linkedListDeque.size();
console.log('Double-ended queue length size = ' + size);
console.log('Deque size = ' + size);
/* Check if the double-ended queue is empty */
/* Check whether the deque is empty */
const isEmpty = linkedListDeque.isEmpty();
console.log('Double-ended queue is empty = ' + isEmpty);
console.log('Is the deque empty = ' + isEmpty);

View File

@@ -6,10 +6,10 @@
const { ListNode } = require('../modules/ListNode');
/* Queue based on linked list implementation */
/* Queue implemented with a linked list */
class LinkedListQueue {
#front; // Front node #front
#rear; // Rear node #rear
#front; // Head node #front
#rear; // Tail node #rear
#queSize = 0;
constructor() {
@@ -17,12 +17,12 @@ class LinkedListQueue {
this.#rear = null;
}
/* Get the length of the queue */
/* Get the queue size */
get size() {
return this.#queSize;
}
/* Check if the queue is empty */
/* Check whether the queue is empty */
isEmpty() {
return this.size === 0;
}
@@ -31,11 +31,11 @@ class LinkedListQueue {
push(num) {
// Add num after the tail node
const node = new ListNode(num);
// If the queue is empty, make both front and rear point to the node
// If the queue is empty, make both the head and tail point to this node
if (!this.#front) {
this.#front = node;
this.#rear = node;
// If the queue is not empty, add the node after the tail node
// If the queue is not empty, add this node after the tail node
} else {
this.#rear.next = node;
this.#rear = node;
@@ -46,19 +46,19 @@ class LinkedListQueue {
/* Dequeue */
pop() {
const num = this.peek();
// Delete head node
// Remove the head node
this.#front = this.#front.next;
this.#queSize--;
return num;
}
/* Return list for printing */
/* Access the front element */
peek() {
if (this.size === 0) throw new Error('Queue is empty');
return this.#front.val;
}
/* Convert linked list to Array and return */
/* Convert the linked list to an Array and return it */
toArray() {
let node = this.#front;
const res = new Array(this.size);
@@ -70,11 +70,11 @@ class LinkedListQueue {
}
}
/* Driver Code */
/* Access front of the queue element */
/* Driver code */
/* Initialize the queue */
const queue = new LinkedListQueue();
/* Elements enqueue */
/* Enqueue elements */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -82,18 +82,18 @@ queue.push(5);
queue.push(4);
console.log('Queue queue = ' + queue.toArray());
/* Return list for printing */
/* Access the front element */
const peek = queue.peek();
console.log('Front element peek = ' + peek);
/* Element dequeue */
/* Dequeue an element */
const pop = queue.pop();
console.log('Dequeue element pop = ' + pop + ', after dequeue, queue = ' + queue.toArray());
console.log('Dequeued element pop = ' + pop + '; after dequeuing, queue = ' + queue.toArray());
/* Get the length of the queue */
/* Get the queue size */
const size = queue.size;
console.log('Queue length size = ' + size);
console.log('Queue size = ' + size);
/* Check if the queue is empty */
/* Check whether the queue is empty */
const isEmpty = queue.isEmpty();
console.log('Queue is empty = ' + isEmpty);
console.log('Is the queue empty = ' + isEmpty);

View File

@@ -6,21 +6,21 @@
const { ListNode } = require('../modules/ListNode');
/* Stack based on linked list implementation */
/* Stack implemented with a linked list */
class LinkedListStack {
#stackPeek; // Use head node as stack top
#stkSize = 0; // Stack length
#stackPeek; // Use the head node as the stack top
#stkSize = 0; // Stack size
constructor() {
this.#stackPeek = null;
}
/* Get the length of the stack */
/* Get the stack size */
get size() {
return this.#stkSize;
}
/* Check if the stack is empty */
/* Check whether the stack is empty */
isEmpty() {
return this.size === 0;
}
@@ -41,13 +41,13 @@ class LinkedListStack {
return num;
}
/* Return list for printing */
/* Access the top element */
peek() {
if (!this.#stackPeek) throw new Error('Stack is empty');
return this.#stackPeek.val;
}
/* Convert linked list to Array and return */
/* Convert the linked list to an Array and return it */
toArray() {
let node = this.#stackPeek;
const res = new Array(this.size);
@@ -59,11 +59,11 @@ class LinkedListStack {
}
}
/* Driver Code */
/* Access top of the stack element */
/* Driver code */
/* Initialize the stack */
const stack = new LinkedListStack();
/* Elements push onto stack */
/* Push elements onto the stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -71,18 +71,18 @@ stack.push(5);
stack.push(4);
console.log('Stack stack = ' + stack.toArray());
/* Return list for printing */
/* Access the top element */
const peek = stack.peek();
console.log('Stack top element peek = ' + peek);
console.log('Top element peek = ' + peek);
/* Element pop from stack */
/* Pop an element from the stack */
const pop = stack.pop();
console.log('Pop element pop = ' + pop + ', after pop, stack = ' + stack.toArray());
console.log('Popped element pop = ' + pop + '; after popping, stack = ' + stack.toArray());
/* Get the length of the stack */
/* Get the stack size */
const size = stack.size;
console.log('Stack length size = ' + size);
console.log('Stack size = ' + size);
/* Check if empty */
/* Check whether it is empty */
const isEmpty = stack.isEmpty();
console.log('Stack is empty = ' + isEmpty);
console.log('Is the stack empty = ' + isEmpty);

View File

@@ -4,12 +4,12 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* Driver Code */
/* Access front of the queue element */
// JavaScript has no built-in queue, can use Array as queue
/* Driver code */
/* Initialize the queue */
// JavaScript has no built-in queue; an Array can be used as a queue
const queue = [];
/* Elements enqueue */
/* Enqueue elements */
queue.push(1);
queue.push(3);
queue.push(2);
@@ -17,19 +17,19 @@ queue.push(5);
queue.push(4);
console.log('Queue queue =', queue);
/* Return list for printing */
/* Access the front element */
const peek = queue[0];
console.log('Front element peek =', peek);
/* Element dequeue */
// Underlying is array, so shift() method has O(n) time complexity
/* Dequeue an element */
// The underlying structure is an array, so shift() has O(n) time complexity
const pop = queue.shift();
console.log('Dequeue element pop =', pop, ', after dequeue, queue = ', queue);
console.log('Dequeued element pop =', pop, '; after dequeuing, queue = ', queue);
/* Get the length of the queue */
/* Get the queue size */
const size = queue.length;
console.log('Queue length size =', size);
console.log('Queue size =', size);
/* Check if the queue is empty */
/* Check whether the queue is empty */
const isEmpty = queue.length === 0;
console.log('Queue is empty = ', isEmpty);
console.log('Is the queue empty = ', isEmpty);

View File

@@ -4,12 +4,12 @@
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
*/
/* Driver Code */
/* Access top of the stack element */
// JavaScript has no built-in stack class, can use Array as stack
/* Driver code */
/* Initialize the stack */
// JavaScript has no built-in stack class; an Array can be used as a stack
const stack = [];
/* Elements push onto stack */
/* Push elements onto the stack */
stack.push(1);
stack.push(3);
stack.push(2);
@@ -17,19 +17,19 @@ stack.push(5);
stack.push(4);
console.log('Stack stack =', stack);
/* Return list for printing */
/* Access the top element */
const peek = stack[stack.length - 1];
console.log('Stack top element peek =', peek);
console.log('Top element peek =', peek);
/* Element pop from stack */
/* Pop an element from the stack */
const pop = stack.pop();
console.log('Pop element pop =', pop);
console.log('After pop, stack =', stack);
console.log('Popped element pop =', pop);
console.log('After popping, stack =', stack);
/* Get the length of the stack */
/* Get the stack size */
const size = stack.length;
console.log('Stack length size =', size);
console.log('Stack size =', size);
/* Check if empty */
/* Check whether it is empty */
const isEmpty = stack.length === 0;
console.log('Is stack empty =', isEmpty);
console.log('Is the stack empty =', isEmpty);