Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. class Node {
  2. /// value;
  3. /// next;
  4. constructor(value) {
  5. this.value = value;
  6. // TODO: Remove this when targeting Node.js 12.
  7. this.next = undefined;
  8. }
  9. }
  10. class Queue {
  11. // TODO: Use private class fields when targeting Node.js 12.
  12. // #_head;
  13. // #_tail;
  14. // #_size;
  15. constructor() {
  16. this.clear();
  17. }
  18. enqueue(value) {
  19. const node = new Node(value);
  20. if (this._head) {
  21. this._tail.next = node;
  22. this._tail = node;
  23. } else {
  24. this._head = node;
  25. this._tail = node;
  26. }
  27. this._size++;
  28. }
  29. dequeue() {
  30. const current = this._head;
  31. if (!current) {
  32. return;
  33. }
  34. this._head = this._head.next;
  35. this._size--;
  36. return current.value;
  37. }
  38. clear() {
  39. this._head = undefined;
  40. this._tail = undefined;
  41. this._size = 0;
  42. }
  43. get size() {
  44. return this._size;
  45. }
  46. * [Symbol.iterator]() {
  47. let current = this._head;
  48. while (current) {
  49. yield current.value;
  50. current = current.next;
  51. }
  52. }
  53. }
  54. module.exports = Queue;