A Queue is a linear data structure that follows the First In, First Out (FIFO) principle. This means that the element inserted first is removed first. One of the simplest ways to implement a queue is by using an array. In an array-based queue, elements are stored in consecutive positions, while two variables called front and rear are used to manage insertion and deletion operations. This implementation is easy to understand and is commonly used when learning the fundamentals of data structures. In this article, you will learn what a queue using an array is, how it works, its operations, implementation using JavaScript, time complexity, advantages, disadvantages, and applications.
Table of Contents
What Is a Queue Using an Array?
A Queue Using an Array is an implementation of the queue data structure in which elements are stored in an array.
It follows the FIFO (First In, First Out) principle. The first element added to the queue is the first element removed.
Two variables are commonly used to manage the queue:
- Front: Keeps track of the first element in the queue.
- Rear: Keeps track of the last element in the queue.
Insertion takes place at the rear, while deletion takes place at the front.
For example, consider a queue containing the elements 10, 20, and 30. If a new element 40 is inserted, it is added after 30. If a deletion operation is performed, 10 is removed first because it was inserted earliest.
How Does a Queue Using an Array Work?
An array-based queue stores elements in a fixed number of positions or in a dynamically allocated array.
The queue uses the front and rear variables to identify where elements should be removed and inserted.
The basic working process is:
- Initially, the queue is empty.
- When an element is inserted, it is added at the rear.
- The rear index is updated after insertion.
- When an element is deleted, the element at the front is removed.
- The front index is updated after deletion.
- The process continues until the queue becomes empty.
In a simple linear array implementation, the rear index moves forward during insertion. Once it reaches the final array position, no more elements can be inserted, even if earlier positions have become empty after deletion. This limitation is called false overflow.
A circular queue solves this problem by allowing the rear index to wrap around and reuse available positions.
Front and Rear in an Array-Based Queue
The front and rear variables are important for managing an array-based queue.
- Front: The front variable stores the index of the first element in the queue. It is used during deletion and peek operations.
- Rear: The rear variable stores the index of the last element in the queue. It is used during insertion.
- Initial Condition: In a common implementation, both front and rear are initialized to -1 to indicate that the queue is empty.
- Empty Condition: The queue is empty when front is equal to -1.
- Full Condition: For a fixed-size linear queue, the queue is full when rear reaches the last valid array index.
Operations of Queue Using Array
An array-based queue supports several important operations.
- Enqueue Operation: The enqueue operation inserts a new element at the rear of the queue. Before insertion, the implementation checks whether the queue is full. If space is available, the rear index is updated, and the new element is inserted.
- Dequeue Operation: The dequeue operation removes the element from the front of the queue. Before deletion, the implementation checks whether the queue is empty. If the queue contains only one element, both front and rear are reset to -1.
- Peek Operation: The peek operation returns the element at the front without removing it.
- IsEmpty Operation: This operation checks whether the queue contains any elements.
- IsFull Operation: This operation checks whether the rear index has reached the last position of the fixed-size array.
- Display Operation: This operation displays all the elements currently present in the queue, starting from the front and ending at the rear.
Implementation of Queue Using Array in JavaScript
The following JavaScript program demonstrates how to implement a linear queue using an array. The queue has a fixed capacity, and the front and rear variables are used to manage its elements.
// Implement a linear queue using an array in JavaScript.
class Queue {
constructor(capacity) {
this.items = new Array(capacity);
this.capacity = capacity;
this.front = -1;
this.rear = -1;
}
enqueue(element) {
if (this.isFull()) {
return "Queue Overflow";
}
if (this.front === -1) {
this.front = 0;
}
this.rear++;
this.items[this.rear] = element;
return `${element} inserted`;
}
dequeue() {
if (this.isEmpty()) {
return "Queue Underflow";
}
const removedElement = this.items[this.front];
if (this.front === this.rear) {
this.front = -1;
this.rear = -1;
} else {
this.front++;
}
return removedElement;
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[this.front];
}
isEmpty() {
return this.front === -1;
}
isFull() {
return this.rear === this.capacity - 1;
}
display() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items.slice(
this.front,
this.rear + 1
);
}
}
const queue = new Queue(5);
console.log(queue.enqueue(10));
console.log(queue.enqueue(20));
console.log(queue.enqueue(30));
console.log("Queue:", queue.display());
console.log("Front Element:", queue.peek());
console.log("Removed:", queue.dequeue());
console.log("Queue After Deletion:", queue.display());
Output:
10 inserted
20 inserted
30 inserted
Queue: [ 10, 20, 30 ]
Front Element: 10
Removed: 10
Queue After Deletion: [ 20, 30 ]
Explanation:
- The constructor() initializes an array with a fixed capacity and sets both front and rear to -1.
- The enqueue() method inserts an element at the rear after checking whether the queue is full.
- The dequeue() method removes the front element and updates the front index.
- If the last element is removed, both front and rear are reset to -1.
- The peek() method returns the front element without deleting it.
- The isEmpty() method checks whether the queue contains any elements.
- The isFull() method checks whether the rear index has reached the last array position.
- The display() method returns the elements currently present in the queue.
In this example, the elements 10, 20, and 30 are inserted into the queue. The peek operation returns 10 because it is the first element. The dequeue operation then removes 10, leaving 20 and 30 in the queue.
Note: This is a linear queue implementation. Deleted positions at the beginning of the array are not reused until the queue becomes completely empty. A circular queue is a better choice when available array positions need to be reused efficiently.
Time Complexity of Queue Using Array
The time complexity depends on how the queue is implemented. For the fixed-size linear queue described above, the operations have the following time complexities:
| Operation | Time Complexity |
|---|---|
| Enqueue | O(1) |
| Dequeue | O(1) |
| Peek | O(1) |
| IsEmpty | O(1) |
| IsFull | O(1) |
| Display | O(n) |
Here, n represents the number of elements currently stored in the queue. Enqueue and dequeue operations take O(1) time because they update the rear and front indices without shifting the remaining elements. The display operation takes O(n) time because it needs to access all elements in the queue.
Advantages of Queue Using Array
An array-based queue provides several advantages.
- Simple Implementation: It is easy to understand and implement using basic array operations.
- Fast Operations: Enqueue and dequeue operations take O(1) time when managed using front and rear indices.
- Direct Index Access: Elements can be accessed using their array positions.
- Contiguous Memory: Array elements are stored in contiguous memory locations, which can improve cache performance.
- Low Memory Overhead: It does not require additional pointer variables for every element.
Disadvantages of Queue Using Array
Despite its advantages, an array-based queue has some limitations.
- Fixed Capacity: A fixed-size array cannot store more elements than its allocated capacity.
- Memory Wastage: Unused array positions may occupy memory even when the queue contains fewer elements.
- False Overflow: A linear queue may report that it is full even when deleted positions are available at the beginning.
- Limited Flexibility: Increasing the capacity of a fixed-size array requires creating a larger array and copying elements.
- Position Management: Front and rear indices must be updated carefully to avoid incorrect operations.
Applications of Queue Using Array
Array-based queues are useful in different programming and computing applications.
- CPU Scheduling: Queues can store processes waiting for CPU execution. In First Come, First Served scheduling, processes are handled in their arrival order.
- Printer Scheduling: Printing requests can be stored in a queue and processed one at a time in the order they arrive.
- Breadth-First Search (BFS): Graph traversal algorithms use queues to store nodes waiting to be visited. BFS explores nodes level by level.
- Network Buffering: Queues can temporarily store data packets before they are transmitted or processed by network devices.
- Task Management: Applications can use queues to organize background tasks that need to be processed in sequence.
- Customer Service Systems: Customer requests can be stored in a queue and handled as service agents become available.
Common Mistakes to Avoid
- Incorrect Initial Values: Initialize both front and rear to -1 when using this implementation's empty-queue convention.
- Ignoring Queue Overflow: Always check whether the rear has reached the last valid array index before inserting an element.
- Ignoring Queue Underflow: Check whether the queue is empty before performing a dequeue operation.
- Forgetting the Last Element Condition: Reset both front and rear to -1 when the final element is removed.
- Confusing Linear and Circular Queues: A linear queue does not reuse deleted positions at the beginning of the array, while a circular queue can reuse them.
Frequently Asked Questions
1. What is a queue using an array?
A queue using an array is a linear data structure implementation that stores elements in an array and follows the FIFO principle.
2. Which variables are used to implement a queue using an array?
The two main variables are front and rear. Front identifies the first element, while rear identifies the last element.
3. What is the initial value of front and rear in an array-based queue?
In a common implementation, both front and rear are initialized to -1 to indicate that the queue is empty.
4. What is the time complexity of enqueue and dequeue operations?
Both operations take O(1) time in a queue implemented using front and rear indices.
5. What is false overflow in a linear queue?
False overflow occurs when the rear reaches the last array position even though some earlier positions have become empty after deletion.
Conclusion
A Queue Using an Array is a simple way to implement the queue data structure. It uses front and rear indices to manage insertion and deletion while following the FIFO principle. Enqueue, dequeue, and peek operations can be performed in constant time when the queue is implemented using index-based management. However, a linear array queue has limitations such as fixed capacity and false overflow. Understanding this implementation provides a strong foundation for learning circular queues and other advanced queue structures.
0 Comments