mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-24 23:50:45 +08:00
Migrate init_array, hash_map_operations, hash_map_traversal, queue_operations, deque_operations and insert_remove_node: zh and ru demo sources gain region markers and the snippets' comments, en, ja and ko are regenerated from reviewed translations, zh-hant via OpenCC. 70 blocks become region references; builtin_hash and five blocks without reviewed translations stay literal. The Java, Kotlin and Rust deque demos follow the book's order, and a few demo comments and typos are aligned with the snippets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
61 lines
2.4 KiB
JavaScript
61 lines
2.4 KiB
JavaScript
/**
|
||
* File: deque.js
|
||
* Created Time: 2023-01-17
|
||
* Author: Zhuo Qinyue ([email protected])
|
||
*/
|
||
|
||
/* Driver Code */
|
||
// [region]{init_deque}
|
||
/* Инициализация двусторонней очереди */
|
||
// В JavaScript нет встроенной двусторонней очереди, поэтому Array можно использовать как двустороннюю очередь
|
||
const deque = [];
|
||
// [endregion]{init_deque}
|
||
|
||
// [region]{push}
|
||
/* Добавление элемента в очередь */
|
||
deque.push(2);
|
||
deque.push(5);
|
||
deque.push(4);
|
||
// Обратите внимание: поскольку используется массив, временная сложность метода unshift() равна O(n)
|
||
deque.unshift(3);
|
||
deque.unshift(1);
|
||
// [endregion]{push}
|
||
console.log('Двусторонняя очередь deque = ', deque);
|
||
|
||
// [region]{peek_first}
|
||
/* Доступ к элементу */
|
||
const peekFirst = deque[0];
|
||
// [endregion]{peek_first}
|
||
console.log('Первый элемент peekFirst = ' + peekFirst);
|
||
// [region]{peek_last}
|
||
const peekLast = deque[deque.length - 1];
|
||
// [endregion]{peek_last}
|
||
console.log('Последний элемент peekLast = ' + peekLast);
|
||
|
||
// [region]{pop_first}
|
||
/* Извлечение элемента из очереди */
|
||
// Обратите внимание: поскольку используется массив, временная сложность метода shift() равна O(n)
|
||
const popFront = deque.shift();
|
||
// [endregion]{pop_first}
|
||
console.log(
|
||
'Извлеченный из головы элемент popFront = ' + popFront + ', deque после извлечения из головы = ' + deque
|
||
);
|
||
// [region]{pop_last}
|
||
const popBack = deque.pop();
|
||
// [endregion]{pop_last}
|
||
console.log(
|
||
'Извлеченный из хвоста элемент popBack = ' + popBack + ', deque после извлечения из хвоста = ' + deque
|
||
);
|
||
|
||
// [region]{size}
|
||
/* Получение длины двусторонней очереди */
|
||
const size = deque.length;
|
||
// [endregion]{size}
|
||
console.log('Длина двусторонней очереди size = ' + size);
|
||
|
||
// [region]{is_empty}
|
||
/* Проверка, пуста ли двусторонняя очередь */
|
||
const isEmpty = size === 0;
|
||
// [endregion]{is_empty}
|
||
console.log('Пуста ли двусторонняя очередь = ' + isEmpty);
|