Queue

Yangshun TayEx-Meta Staff Engineer
语言

Implement a queue data structure in JavaScript that contains the following operations:

  • new Queue(): Creates an instance of a Queue class that doesn't contain any items. The constructor does not accept any arguments.
  • enqueue(item): Adds item to the back of the queue and returns the new number of items. Required time complexity: O(1).
  • dequeue(): Removes and returns the item at the front of the queue, or returns undefined if the queue is empty. Required time complexity: O(1).
  • isEmpty(): Determines if the queue is empty. Required time complexity: O(1).
  • front(): Returns the item at the front of the queue without removing it, or returns undefined if the queue is empty. Required time complexity: O(1).
  • back(): Returns the item at the back of the queue without removing it, or returns undefined if the queue is empty. Required time complexity: O(1).
  • length(): Returns the number of items in the queue. Required time complexity: O(1).

Examples

const queue = new Queue();
queue.isEmpty(); // true
queue.enqueue(1);
queue.enqueue(2);
queue.length(); // 2
queue.enqueue(3);
queue.front(); // 1
queue.back(); // 3
queue.dequeue(); // 1
queue.isEmpty(); // false

Hints

加载编辑器

    Queue | 算法面试题及解决方案