mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-07 04:30:43 +08:00
Translate all code to English (#1836)
* 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
This commit is contained in:
156
en/codes/javascript/chapter_stack_and_queue/array_deque.js
Normal file
156
en/codes/javascript/chapter_stack_and_queue/array_deque.js
Normal file
@@ -0,0 +1,156 @@
|
||||
/**
|
||||
* 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);
|
||||
106
en/codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
106
en/codes/javascript/chapter_stack_and_queue/array_queue.js
Normal file
@@ -0,0 +1,106 @@
|
||||
/**
|
||||
* File: array_queue.js
|
||||
* Created Time: 2022-12-13
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* Queue based on circular array implementation */
|
||||
class ArrayQueue {
|
||||
#nums; // Array for storing queue elements
|
||||
#front = 0; // Front pointer, points to the front of the queue element
|
||||
#queSize = 0; // Queue length
|
||||
|
||||
constructor(capacity) {
|
||||
this.#nums = new Array(capacity);
|
||||
}
|
||||
|
||||
/* Get the capacity of the queue */
|
||||
get capacity() {
|
||||
return this.#nums.length;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
isEmpty() {
|
||||
return this.#queSize === 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
push(num) {
|
||||
if (this.size === this.capacity) {
|
||||
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
|
||||
const rear = (this.#front + this.size) % this.capacity;
|
||||
// Front pointer moves one position backward
|
||||
this.#nums[rear] = num;
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// Move front pointer backward by one position, if it passes the tail, return to array head
|
||||
this.#front = (this.#front + 1) % this.capacity;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
peek() {
|
||||
if (this.isEmpty()) throw new Error('Queue is empty');
|
||||
return this.#nums[this.#front];
|
||||
}
|
||||
|
||||
/* Return Array */
|
||||
toArray() {
|
||||
// Elements enqueue
|
||||
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];
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access front of the queue element */
|
||||
const capacity = 10;
|
||||
const queue = new ArrayQueue(capacity);
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('Queue queue =', queue.toArray());
|
||||
|
||||
/* Return list for printing */
|
||||
const peek = queue.peek();
|
||||
console.log('Front element peek = ' + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
const pop = queue.pop();
|
||||
console.log('Dequeue element pop = ' + pop + ', after dequeue queue =', queue.toArray());
|
||||
|
||||
/* Get the length of the queue */
|
||||
const size = queue.size;
|
||||
console.log('Queue length size = ' + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('Queue is empty = ' + isEmpty);
|
||||
|
||||
/* Test 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());
|
||||
}
|
||||
75
en/codes/javascript/chapter_stack_and_queue/array_stack.js
Normal file
75
en/codes/javascript/chapter_stack_and_queue/array_stack.js
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* File: array_stack.js
|
||||
* Created Time: 2022-12-09
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
/* Stack based on array implementation */
|
||||
class ArrayStack {
|
||||
#stack;
|
||||
constructor() {
|
||||
this.#stack = [];
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
get size() {
|
||||
return this.#stack.length;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
isEmpty() {
|
||||
return this.#stack.length === 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
push(num) {
|
||||
this.#stack.push(num);
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
pop() {
|
||||
if (this.isEmpty()) throw new Error('Stack is empty');
|
||||
return this.#stack.pop();
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
top() {
|
||||
if (this.isEmpty()) throw new Error('Stack is empty');
|
||||
return this.#stack[this.#stack.length - 1];
|
||||
}
|
||||
|
||||
/* Return Array */
|
||||
toArray() {
|
||||
return this.#stack;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access top of the stack element */
|
||||
const stack = new ArrayStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('Stack stack = ');
|
||||
console.log(stack.toArray());
|
||||
|
||||
/* Return list for printing */
|
||||
const top = stack.top();
|
||||
console.log('Stack top element top = ' + top);
|
||||
|
||||
/* Element pop from stack */
|
||||
const pop = stack.pop();
|
||||
console.log('Pop element pop = ' + pop + ', after pop, stack = ');
|
||||
console.log(stack.toArray());
|
||||
|
||||
/* Get the length of the stack */
|
||||
const size = stack.size;
|
||||
console.log('Stack length size = ' + size);
|
||||
|
||||
/* Check if empty */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('Stack is empty = ' + isEmpty);
|
||||
44
en/codes/javascript/chapter_stack_and_queue/deque.js
Normal file
44
en/codes/javascript/chapter_stack_and_queue/deque.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* File: deque.js
|
||||
* Created Time: 2023-01-17
|
||||
* 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
|
||||
const deque = [];
|
||||
|
||||
/* Elements enqueue */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
// Note: due to array, unshift() method has O(n) time complexity
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
console.log('Double-ended queue deque = ', deque);
|
||||
|
||||
/* Update 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
|
||||
const popFront = deque.shift();
|
||||
console.log(
|
||||
'Front dequeue element popFront = ' + popFront + ', after front dequeue, deque = ' + deque
|
||||
);
|
||||
const popBack = deque.pop();
|
||||
console.log(
|
||||
'Dequeue rear element popBack = ' + popBack + ', after rear dequeue, deque = ' + deque
|
||||
);
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
const size = deque.length;
|
||||
console.log('Double-ended queue length size = ' + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
const isEmpty = size === 0;
|
||||
console.log('Double-ended queue is empty = ' + isEmpty);
|
||||
167
en/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js
Normal file
167
en/codes/javascript/chapter_stack_and_queue/linkedlist_deque.js
Normal file
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: linkedlist_deque.js
|
||||
* Created Time: 2023-02-04
|
||||
* Author: Zhuo Qinyue (1403450829@qq.com)
|
||||
*/
|
||||
|
||||
/* Doubly linked list node */
|
||||
class ListNode {
|
||||
prev; // Predecessor node reference (pointer)
|
||||
next; // Successor node reference (pointer)
|
||||
val; // Node value
|
||||
|
||||
constructor(val) {
|
||||
this.val = val;
|
||||
this.next = null;
|
||||
this.prev = null;
|
||||
}
|
||||
}
|
||||
|
||||
/* Double-ended queue based on doubly linked list implementation */
|
||||
class LinkedListDeque {
|
||||
#front; // Head node front
|
||||
#rear; // Tail node rear
|
||||
#queSize; // Length of the double-ended queue
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
this.#queSize = 0;
|
||||
}
|
||||
|
||||
/* Rear of the queue enqueue operation */
|
||||
pushLast(val) {
|
||||
const node = new ListNode(val);
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// Add node to the tail of the linked list
|
||||
this.#rear.next = node;
|
||||
node.prev = this.#rear;
|
||||
this.#rear = node; // Update tail node
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* Front of the queue enqueue operation */
|
||||
pushFirst(val) {
|
||||
const node = new ListNode(val);
|
||||
// If the linked list is empty, make both front and rear point to node
|
||||
if (this.#queSize === 0) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
} else {
|
||||
// Add node to the head of the linked list
|
||||
this.#front.prev = node;
|
||||
node.next = this.#front;
|
||||
this.#front = node; // Update head node
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* Temporarily store tail node value */
|
||||
popLast() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#rear.val; // Store tail node value
|
||||
// Update tail node
|
||||
let temp = this.#rear.prev;
|
||||
if (temp !== null) {
|
||||
temp.next = null;
|
||||
this.#rear.prev = null;
|
||||
}
|
||||
this.#rear = temp; // Update tail node
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* Temporarily store head node value */
|
||||
popFirst() {
|
||||
if (this.#queSize === 0) {
|
||||
return null;
|
||||
}
|
||||
const value = this.#front.val; // Store tail node value
|
||||
// Delete head node
|
||||
let temp = this.#front.next;
|
||||
if (temp !== null) {
|
||||
temp.prev = null;
|
||||
this.#front.next = null;
|
||||
}
|
||||
this.#front = temp; // Update head node
|
||||
this.#queSize--;
|
||||
return value;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
peekLast() {
|
||||
return this.#queSize === 0 ? null : this.#rear.val;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
peekFirst() {
|
||||
return this.#queSize === 0 ? null : this.#front.val;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* Print deque */
|
||||
print() {
|
||||
const arr = [];
|
||||
let temp = this.#front;
|
||||
while (temp !== null) {
|
||||
arr.push(temp.val);
|
||||
temp = temp.next;
|
||||
}
|
||||
console.log('[' + arr.join(', ') + ']');
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Get the length of the double-ended queue */
|
||||
const linkedListDeque = new LinkedListDeque();
|
||||
linkedListDeque.pushLast(3);
|
||||
linkedListDeque.pushLast(2);
|
||||
linkedListDeque.pushLast(5);
|
||||
console.log('Deque linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* Update element */
|
||||
const peekFirst = linkedListDeque.peekFirst();
|
||||
console.log('Front element peekFirst = ' + peekFirst);
|
||||
const peekLast = linkedListDeque.peekLast();
|
||||
console.log('Rear element peekLast = ' + peekLast);
|
||||
|
||||
/* Elements enqueue */
|
||||
linkedListDeque.pushLast(4);
|
||||
console.log('After element 4 enqueues at rear, linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
linkedListDeque.pushFirst(1);
|
||||
console.log('After element 1 enqueues at front, linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* Element dequeue */
|
||||
const popLast = linkedListDeque.popLast();
|
||||
console.log('Rear dequeue element = ' + popLast + ', after rear dequeue linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
const popFirst = linkedListDeque.popFirst();
|
||||
console.log('Front dequeue element = ' + popFirst + ', after front dequeue linkedListDeque = ');
|
||||
linkedListDeque.print();
|
||||
|
||||
/* Get the length of the double-ended queue */
|
||||
const size = linkedListDeque.size();
|
||||
console.log('Double-ended queue length size = ' + size);
|
||||
|
||||
/* Check if the double-ended queue is empty */
|
||||
const isEmpty = linkedListDeque.isEmpty();
|
||||
console.log('Double-ended queue is empty = ' + isEmpty);
|
||||
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* File: linkedlist_queue.js
|
||||
* Created Time: 2022-12-20
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* Queue based on linked list implementation */
|
||||
class LinkedListQueue {
|
||||
#front; // Front node #front
|
||||
#rear; // Rear node #rear
|
||||
#queSize = 0;
|
||||
|
||||
constructor() {
|
||||
this.#front = null;
|
||||
this.#rear = null;
|
||||
}
|
||||
|
||||
/* Get the length of the queue */
|
||||
get size() {
|
||||
return this.#queSize;
|
||||
}
|
||||
|
||||
/* Check if the queue is empty */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* Enqueue */
|
||||
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 (!this.#front) {
|
||||
this.#front = node;
|
||||
this.#rear = node;
|
||||
// If the queue is not empty, add the node after the tail node
|
||||
} else {
|
||||
this.#rear.next = node;
|
||||
this.#rear = node;
|
||||
}
|
||||
this.#queSize++;
|
||||
}
|
||||
|
||||
/* Dequeue */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
// Delete head node
|
||||
this.#front = this.#front.next;
|
||||
this.#queSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
peek() {
|
||||
if (this.size === 0) throw new Error('Queue is empty');
|
||||
return this.#front.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
toArray() {
|
||||
let node = this.#front;
|
||||
const res = new Array(this.size);
|
||||
for (let i = 0; i < res.length; i++) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access front of the queue element */
|
||||
const queue = new LinkedListQueue();
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('Queue queue = ' + queue.toArray());
|
||||
|
||||
/* Return list for printing */
|
||||
const peek = queue.peek();
|
||||
console.log('Front element peek = ' + peek);
|
||||
|
||||
/* Element dequeue */
|
||||
const pop = queue.pop();
|
||||
console.log('Dequeue element pop = ' + pop + ', after dequeue, queue = ' + queue.toArray());
|
||||
|
||||
/* Get the length of the queue */
|
||||
const size = queue.size;
|
||||
console.log('Queue length size = ' + size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
const isEmpty = queue.isEmpty();
|
||||
console.log('Queue is empty = ' + isEmpty);
|
||||
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* File: linkedlist_stack.js
|
||||
* Created Time: 2022-12-22
|
||||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||||
*/
|
||||
|
||||
const { ListNode } = require('../modules/ListNode');
|
||||
|
||||
/* Stack based on linked list implementation */
|
||||
class LinkedListStack {
|
||||
#stackPeek; // Use head node as stack top
|
||||
#stkSize = 0; // Stack length
|
||||
|
||||
constructor() {
|
||||
this.#stackPeek = null;
|
||||
}
|
||||
|
||||
/* Get the length of the stack */
|
||||
get size() {
|
||||
return this.#stkSize;
|
||||
}
|
||||
|
||||
/* Check if the stack is empty */
|
||||
isEmpty() {
|
||||
return this.size === 0;
|
||||
}
|
||||
|
||||
/* Push */
|
||||
push(num) {
|
||||
const node = new ListNode(num);
|
||||
node.next = this.#stackPeek;
|
||||
this.#stackPeek = node;
|
||||
this.#stkSize++;
|
||||
}
|
||||
|
||||
/* Pop */
|
||||
pop() {
|
||||
const num = this.peek();
|
||||
this.#stackPeek = this.#stackPeek.next;
|
||||
this.#stkSize--;
|
||||
return num;
|
||||
}
|
||||
|
||||
/* Return list for printing */
|
||||
peek() {
|
||||
if (!this.#stackPeek) throw new Error('Stack is empty');
|
||||
return this.#stackPeek.val;
|
||||
}
|
||||
|
||||
/* Convert linked list to Array and return */
|
||||
toArray() {
|
||||
let node = this.#stackPeek;
|
||||
const res = new Array(this.size);
|
||||
for (let i = res.length - 1; i >= 0; i--) {
|
||||
res[i] = node.val;
|
||||
node = node.next;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Access top of the stack element */
|
||||
const stack = new LinkedListStack();
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('Stack stack = ' + stack.toArray());
|
||||
|
||||
/* Return list for printing */
|
||||
const peek = stack.peek();
|
||||
console.log('Stack top element peek = ' + peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
const pop = stack.pop();
|
||||
console.log('Pop element pop = ' + pop + ', after pop, stack = ' + stack.toArray());
|
||||
|
||||
/* Get the length of the stack */
|
||||
const size = stack.size;
|
||||
console.log('Stack length size = ' + size);
|
||||
|
||||
/* Check if empty */
|
||||
const isEmpty = stack.isEmpty();
|
||||
console.log('Stack is empty = ' + isEmpty);
|
||||
35
en/codes/javascript/chapter_stack_and_queue/queue.js
Normal file
35
en/codes/javascript/chapter_stack_and_queue/queue.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: queue.js
|
||||
* Created Time: 2022-12-05
|
||||
* 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
|
||||
const queue = [];
|
||||
|
||||
/* Elements enqueue */
|
||||
queue.push(1);
|
||||
queue.push(3);
|
||||
queue.push(2);
|
||||
queue.push(5);
|
||||
queue.push(4);
|
||||
console.log('Queue queue =', queue);
|
||||
|
||||
/* Return list for printing */
|
||||
const peek = queue[0];
|
||||
console.log('Front element peek =', peek);
|
||||
|
||||
/* Element dequeue */
|
||||
// Underlying is array, so shift() method has O(n) time complexity
|
||||
const pop = queue.shift();
|
||||
console.log('Dequeue element pop =', pop, ', after dequeue, queue = ', queue);
|
||||
|
||||
/* Get the length of the queue */
|
||||
const size = queue.length;
|
||||
console.log('Queue length size =', size);
|
||||
|
||||
/* Check if the queue is empty */
|
||||
const isEmpty = queue.length === 0;
|
||||
console.log('Queue is empty = ', isEmpty);
|
||||
35
en/codes/javascript/chapter_stack_and_queue/stack.js
Normal file
35
en/codes/javascript/chapter_stack_and_queue/stack.js
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* File: stack.js
|
||||
* Created Time: 2022-12-04
|
||||
* 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
|
||||
const stack = [];
|
||||
|
||||
/* Elements push onto stack */
|
||||
stack.push(1);
|
||||
stack.push(3);
|
||||
stack.push(2);
|
||||
stack.push(5);
|
||||
stack.push(4);
|
||||
console.log('Stack stack =', stack);
|
||||
|
||||
/* Return list for printing */
|
||||
const peek = stack[stack.length - 1];
|
||||
console.log('Stack top element peek =', peek);
|
||||
|
||||
/* Element pop from stack */
|
||||
const pop = stack.pop();
|
||||
console.log('Pop element pop =', pop);
|
||||
console.log('After pop, stack =', stack);
|
||||
|
||||
/* Get the length of the stack */
|
||||
const size = stack.length;
|
||||
console.log('Stack length size =', size);
|
||||
|
||||
/* Check if empty */
|
||||
const isEmpty = stack.length === 0;
|
||||
console.log('Is stack empty =', isEmpty);
|
||||
Reference in New Issue
Block a user