mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-15 14:48:05 +08:00
* Add Russian docs site baseline * Add Russian localized codebase * Polish Russian code wording * Update ru code translation. * Update code translation and chapter covers. * Fix pythontutor extraction. * Add README and landing page. * placeholder of profiles * Use figures of English version * Remove chapter paperbook
36 lines
1.3 KiB
JavaScript
36 lines
1.3 KiB
JavaScript
/**
|
||
* File: queue.js
|
||
* Created Time: 2022-12-05
|
||
* Author: S-N-O-R-L-A-X (snorlax.xu@outlook.com)
|
||
*/
|
||
|
||
/* Driver Code */
|
||
/* Инициализация очереди */
|
||
// В JavaScript нет встроенной очереди, поэтому Array можно использовать как очередь
|
||
const queue = [];
|
||
|
||
/* Добавление элемента в очередь */
|
||
queue.push(1);
|
||
queue.push(3);
|
||
queue.push(2);
|
||
queue.push(5);
|
||
queue.push(4);
|
||
console.log('Очередь queue =', queue);
|
||
|
||
/* Доступ к элементу в начале очереди */
|
||
const peek = queue[0];
|
||
console.log('Первый элемент peek =', peek);
|
||
|
||
/* Извлечение элемента из очереди */
|
||
// В основе лежит массив, поэтому временная сложность метода shift() равна O(n)
|
||
const pop = queue.shift();
|
||
console.log('Извлеченный элемент pop =', pop, ', queue после извлечения = ', queue);
|
||
|
||
/* Получение длины очереди */
|
||
const size = queue.length;
|
||
console.log('Длина очереди size =', size);
|
||
|
||
/* Проверка, пуста ли очередь */
|
||
const isEmpty = queue.length === 0;
|
||
console.log('Пуста ли очередь = ', isEmpty);
|