123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 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;
- }
- peek() {
- if (!this.#head) {
- return;
- }
- return this.#head.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;
- }
- }
- }
|