12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- class Node {
- value;
- next;
- constructor(value) {
- this.value = value;
- }
- }
- export default class Queue {
- #head;
- #tail;
- #size;
- constructor() {
- this.clear();
- }
- enqueue(value) {
- const node = new Node(value);
- if (this.#head) {
- this.#tail.next = node;
- this.#tail = node;
- } else {
- this.#head = node;
- this.#tail = node;
- }
- this.#size++;
- }
- dequeue() {
- const current = this.#head;
- if (!current) {
- return;
- }
- this.#head = this.#head.next;
- this.#size--;
- return current.value;
- }
- clear() {
- this.#head = undefined;
- this.#tail = undefined;
- this.#size = 0;
- }
- get size() {
- return this.#size;
- }
- * [Symbol.iterator]() {
- let current = this.#head;
- while (current) {
- yield current.value;
- current = current.next;
- }
- }
- }
|