How to Implement Queue Using Array: Simple Guide
To implement a
queue using an array, use two pointers or indices to track the front and rear positions. Enqueue adds elements at the rear, and dequeue removes elements from the front, maintaining the FIFO order.Syntax
A queue using an array typically has these parts:
- Array: Stores the queue elements.
- Front index: Points to the first element to dequeue.
- Rear index: Points to the last element enqueued.
- Enqueue operation: Adds an element at the rear.
- Dequeue operation: Removes an element from the front.
Indices move forward as elements are added or removed. When the rear reaches the array end, you can wrap around (circular queue) or stop if full.
javascript
class Queue { constructor(size) { this.size = size; this.queue = new Array(size); this.front = -1; this.rear = -1; } enqueue(element) { if (this.rear === this.size - 1) { console.log('Queue is full'); return; } if (this.front === -1) this.front = 0; this.rear++; this.queue[this.rear] = element; } dequeue() { if (this.front === -1 || this.front > this.rear) { console.log('Queue is empty'); return null; } const element = this.queue[this.front]; this.front++; return element; } }
Example
This example shows how to create a queue, add elements, and remove them in FIFO order using an array.
javascript
class Queue { constructor(size) { this.size = size; this.queue = new Array(size); this.front = -1; this.rear = -1; } enqueue(element) { if (this.rear === this.size - 1) { console.log('Queue is full'); return; } if (this.front === -1) this.front = 0; this.rear++; this.queue[this.rear] = element; } dequeue() { if (this.front === -1 || this.front > this.rear) { console.log('Queue is empty'); return null; } const element = this.queue[this.front]; this.front++; return element; } } const q = new Queue(3); q.enqueue(10); q.enqueue(20); q.enqueue(30); q.enqueue(40); // Should show 'Queue is full' console.log(q.dequeue()); // 10 console.log(q.dequeue()); // 20 console.log(q.dequeue()); // 30 console.log(q.dequeue()); // Should show 'Queue is empty' and return null
Output
Queue is full
10
20
30
Queue is empty
null
Common Pitfalls
Common mistakes when implementing a queue with an array include:
- Not handling the queue full condition properly, causing overflow.
- Not updating front and rear indices correctly, leading to incorrect enqueue or dequeue.
- Not resetting indices when the queue becomes empty, causing errors on further operations.
- Using a simple array without wrap-around, which wastes space after some dequeues.
Using a circular queue approach fixes space wastage by wrapping indices back to the start.
javascript
class Queue { constructor(size) { this.size = size; this.queue = new Array(size); this.front = 0; this.rear = 0; this.count = 0; } // Wrong: No check for full queue enqueue(element) { this.queue[this.rear] = element; this.rear = (this.rear + 1) % this.size; this.count++; } // Correct: Check for full queue enqueueSafe(element) { if (this.count === this.size) { console.log('Queue is full'); return; } this.queue[this.rear] = element; this.rear = (this.rear + 1) % this.size; this.count++; } }
Quick Reference
Queue using array key points:
- Enqueue: Add element at rear index, then move rear forward.
- Dequeue: Remove element at front index, then move front forward.
- Check full: When rear reaches array end or count equals size.
- Check empty: When front passes rear or count is zero.
- Circular queue: Use modulo to wrap indices and reuse space.
Key Takeaways
Use front and rear indices to track queue ends in the array.
Always check for full and empty conditions to avoid errors.
Consider circular queue to efficiently use array space.
Update indices carefully after enqueue and dequeue operations.
Reset indices or use count to handle empty queue state properly.