1. /**
  2. * Simple Queue Implementation (for temporary use)
  3. */
  4. class Queue {
  5. constructor() {
  6. this.items = []; // Use an array to store queue elements
  7. }
  8. /**
  9. * Enqueues an element to the end of the queue.
  10. * @param {any} element - The element to enqueue.
  11. */
  12. enqueue(element) {
  13. this.items.push(element);
  14. }
  15. /**
  16. * Dequeues and returns the element at the front of the queue.
  17. * Returns undefined if the queue is empty.
  18. * @returns {any} The dequeued element.
  19. */
  20. dequeue() {
  21. if (this.isEmpty()) {
  22. return undefined;
  23. }
  24. return this.items.shift(); // Remove and return the first element
  25. }
  26. /**
  27. * Returns the element at the front of the queue without removing it.
  28. * Returns undefined if the queue is empty.
  29. * @returns {any} The element at the front.
  30. */
  31. peek() {
  32. if (this.isEmpty()) {
  33. return undefined;
  34. }
  35. return this.items[0];
  36. }
  37. /**
  38. * Checks if the queue is empty.
  39. * @returns {boolean} True if the queue is empty, false otherwise.
  40. */
  41. isEmpty() {
  42. return this.items.length === 0;
  43. }
  44. /**
  45. * Returns the number of elements in the queue.
  46. * @returns {number} The queue's size.
  47. */
  48. size() {
  49. return this.items.length;
  50. }
  51. }
  52. // Export the Queue class (for use in other modules)
  53. export default Queue;

Add your comment