使用js实现一个循环队列

class CircularQueue {
  constructor(capacity) {
    this.capacity = capacity;
    this.queue = new Array(capacity);
    this.head = -1;
    this.tail = -1;
    this.size = 0;
  }

  enqueue(item) {
    if (this.isFull()) {
      return false; // Or throw an error: throw new Error("Queue is full");
    }

    if (this.isEmpty()) {
      this.head = 0;
    }

    this.tail = (this.tail + 1) % this.capacity;
    this.queue[this.tail] = item;
    this.size++;
    return true;
  }

  dequeue() {
    if (this.isEmpty()) {
      return null; // Or throw an error: throw new Error("Queue is empty");
    }

    const removedItem = this.queue[this.head];
    this.head = (this.head + 1) % this.capacity;
    this.size--;

    if (this.isEmpty()) {
      this.head = -1;
      this.tail = -1;
    }

    return removedItem;
  }

  peek() {
    if (this.isEmpty()) {
      return null; // Or throw an error
    }
    return this.queue[this.head];
  }

  isFull() {
    return this.size === this.capacity;
  }

  isEmpty() {
    return this.size === 0;
  }

  getSize() {
    return this.size;
  }

  // Clears the queue
  clear() {
    this.head = -1;
    this.tail = -1;
    this.size = 0;
    this.queue = new Array(this.capacity); // Optional: Reset the array
  }


  // For debugging purposes (optional)
  print() {
    if (this.isEmpty()) {
      console.log("Queue is empty");
    } else {
      let str = "";
      for (let i = 0; i < this.size; i++) {
        str += this.queue[(this.head + i) % this.capacity] + " ";
      }
      console.log(str);
    }
  }
}


// Example usage:
const queue = new CircularQueue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.enqueue(40);
queue.enqueue(50);

console.log(queue.isFull()); // Output: true
console.log(queue.dequeue()); // Output: 10
console.log(queue.dequeue()); // Output: 20
queue.enqueue(60);
queue.print();              // Output: 30 40 50 60
console.log(queue.getSize()); // Output: 4
queue.clear();
console.log(queue.isEmpty()); // Output: true

Key improvements and explanations in this implementation:

  • Clarity and Readability: The code is well-commented and uses descriptive variable names.
  • Error Handling: Includes checks for isFull() and isEmpty() conditions in enqueue and dequeue to prevent unexpected behavior. You can choose to return false/null or throw an error based on your needs.
  • Modulus Operator: Correctly uses the modulus operator (%) to handle wrapping around the array when the tail reaches the end.
  • Size Tracking: Maintains the size of the queue, making isFull() and isEmpty() checks more efficient.
  • Clear Method: Includes a clear() method to reset the queue.
  • Print Method (Optional): A print() method is provided for easy debugging and visualization.
  • Comprehensive Example: The example usage demonstrates the basic operations and edge cases.

This improved version provides a robust and easy-to-understand implementation of a circular queue in JavaScript. Remember to choose the error handling approach (returning null/false or throwing errors) that best suits your application's requirements.

posted @ 2024-12-06 09:33  王铁柱6  阅读(33)  评论(0)    收藏  举报