A simple queue is a linear data structure that follows the FIFO (First In, First Out) principle. This means the element inserted first is the first element removed. In a simple queue, new elements are added at the rear, while existing elements are removed from the front. Simple queues are commonly used in printer scheduling, CPU task management, customer service systems, and other applications where data must be processed in order.
Table of Contents
What Is a Simple Queue?
A simple queue is a basic type of queue in which elements are inserted from one end, called the rear, and removed from the other end, called the front.
It follows the FIFO principle, which ensures that elements are processed in the same order in which they are inserted.
For example, when customers wait in a line at a bank, the customer who arrives first is served first. Similarly, a simple queue processes the first inserted element before the others.
Key points:
- A simple queue is a linear data structure.
- It follows the FIFO principle.
- Insertion takes place at the rear.
- Deletion takes place at the front.
- It maintains the order of inserted elements.
Characteristics of a Simple Queue
A simple queue has several important characteristics that make it useful for managing data.
- FIFO Principle: The first element inserted is the first element removed.
- Two Ends: It maintains two positions called front and rear.
- Insertion at Rear: New elements are always added at the rear.
- Deletion at Front: Elements are always removed from the front.
- Sequential Processing: Elements are processed in their insertion order.
- Fixed or Dynamic Storage: A queue can be implemented using a fixed-size array or a dynamically growing structure such as a linked list.
How Does a Simple Queue Work?
A simple queue works by inserting elements at the rear and removing elements from the front.
The following steps explain its working:
- Initially, the queue is empty.
- When a new element is inserted, it is added at the rear.
- Additional elements are inserted after the previously added elements.
- When a deletion operation is performed, the element at the front is removed.
- The next element becomes the front element.
- The process continues until the queue becomes empty.
For example, if the values 10, 20, and 30 are inserted into a simple queue, the first dequeue operation removes 10. The next dequeue operation removes 20.
Basic Operations of a Simple Queue
A simple queue supports several basic operations to manage its elements.
1. Enqueue: Enqueue is used to insert a new element at the rear of the queue.
2. Dequeue: Dequeue is used to remove an element from the front of the queue.
3. Peek: Peek returns the front element without removing it.
4. Front: The front operation accesses the first element in the queue.
5. Rear: The rear operation accesses the last element in the queue.
6. isEmpty(): This operation checks whether the queue contains any elements.
7. isFull(): This operation checks whether a fixed-size queue has reached its maximum capacity.
Implementation of Simple Queue Using an Array
A simple queue can be implemented using an array. Two variables, front and rear, are used to manage the positions of the first and last elements.
- front stores the index of the first element.
- rear stores the index of the last element.
When an element is inserted, the rear index is increased. When an element is removed, the front index is increased.
JavaScript Example:
// This program demonstrates a simple queue using an array.
class Queue {
constructor() {
this.queue = [];
}
enqueue(element) {
this.queue.push(element);
console.log(element + " inserted");
}
dequeue() {
if (this.queue.length === 0) {
console.log("Queue is empty");
return;
}
let removed = this.queue.shift();
console.log("Removed:", removed);
}
peek() {
if (this.queue.length === 0) {
console.log("Queue is empty");
return;
}
console.log("Front Element:", this.queue[0]);
}
display() {
console.log("Queue:", this.queue);
}
}
let queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
queue.peek();
queue.dequeue();
queue.display();
Output:
10 inserted
20 inserted
30 inserted
Front Element: 10
Removed: 10
Queue: [20, 30]
In this example, the Queue class contains methods for inserting, removing, accessing, and displaying elements. The push() method inserts elements at the rear, while shift() removes elements from the front.
Note: This implementation is suitable for learning queue operations. However, JavaScript's shift() method generally takes O(n) time because the remaining array elements need to be reindexed.
Implementation of Simple Queue Using a Linked List
A simple queue can also be implemented using a linked list. In this implementation, each node stores data and a reference to the next node.
Two pointers are maintained:
- front points to the first node.
- rear points to the last node.
When a new element is inserted, a node is added at the rear. During deletion, the front pointer moves to the next node.
JavaScript Example:
// This program demonstrates a simple queue using a linked list.
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
}
enqueue(data) {
let newNode = new Node(data);
if (this.rear === null) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
}
dequeue() {
if (this.front === null) {
console.log("Queue is empty");
return;
}
let removed = this.front.data;
this.front = this.front.next;
if (this.front === null) {
this.rear = null;
}
return removed;
}
display() {
let current = this.front;
let elements = [];
while (current !== null) {
elements.push(current.data);
current = current.next;
}
console.log(elements);
}
}
let queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log("Removed:", queue.dequeue());
queue.display();
Output:
Removed: 10
[20, 30]
In this example, each element is stored in a separate node. The enqueue operation adds a new node at the rear, while the dequeue operation removes the front node by updating the front pointer.
Algorithm of Simple Queue
The following algorithms explain the basic insertion and deletion operations in an array-based simple queue.
Enqueue Algorithm:
- Start.
- Check whether rear == MAX - 1.
- If true, display "Queue Overflow".
- Otherwise, check whether front == -1.
- If true, set front = 0.
- Increase rear by 1.
- Insert the new element at queue[rear].
- Stop.
Dequeue Algorithm:
- Start.
- Check whether front == -1 or front > rear.
- If true, display "Queue Underflow".
- Otherwise, store the element at queue[front].
- If front == rear, set both front and rear to -1.
- Otherwise, increase front by 1.
- Return the removed element.
- Stop.
These algorithms help maintain the correct FIFO order during insertion and deletion.
Time Complexity of Simple Queue
Time complexity describes how the execution time of an operation changes as the number of elements increases.
The complexity depends on the implementation used.
| Operation | Array using push/shift | Linked List with Front and Rear |
|---|---|---|
| Enqueue | O(1) amortized | O(1) |
| Dequeue | O(n) | O(1) |
| Peek | O(1) | O(1) |
| isEmpty() | O(1) | O(1) |
| Display | O(n) | O(n) |
A linked-list implementation with front and rear pointers provides O(1) enqueue and dequeue operations. A basic JavaScript array implementation using shift() takes O(n) time for dequeue.
Advantages of Simple Queue
A simple queue provides several benefits when managing data.
- Easy to Implement: It is simple to understand and implement using arrays or linked lists.
- Maintains FIFO Order: It processes elements in the order in which they are inserted.
- Organized Processing: It helps manage tasks and requests systematically.
- Useful in Scheduling: It supports applications that process waiting tasks.
- Flexible Implementation: It can be implemented using different data structures.
Disadvantages of Simple Queue
Although a simple queue is useful, it also has some limitations.
- Limited Insertion and Deletion: Insertion is allowed only at the rear, and deletion is allowed only at the front.
- Wasted Array Space: In a linear array-based queue, positions freed at the beginning may not be reused without shifting elements.
- Fixed Capacity: An array-based queue with a fixed size cannot store more elements than its capacity.
- Less Flexible: It does not support insertion and deletion at both ends like a deque.
- Overflow Condition: A fixed-size queue cannot accept new elements when it becomes full.
Applications of Simple Queue
Simple queues are used in many real-world and computer science applications.
- Printer Scheduling: Print requests are processed in the order in which they are received.
- CPU Scheduling: Processes can wait in a queue for their turn to execute.
- Customer Service Systems: Customers are served according to their arrival order.
- Network Communication: Queues temporarily store data packets waiting to be transmitted or processed.
- Task Scheduling: Applications use queues to manage tasks waiting for execution.
- Breadth-First Search (BFS): Graph and tree traversal algorithms use queues to process nodes level by level.
Common Mistakes to Avoid
- Confusing Front and Rear: Remember that insertion happens at the rear and deletion happens at the front.
- Ignoring Queue Underflow: Always check whether the queue is empty before removing an element.
- Ignoring Queue Overflow: Check the capacity before inserting elements into a fixed-size queue.
- Incorrect Pointer Updates: Update front and rear correctly when inserting or removing elements.
- Assuming Every Queue Implementation Has the Same Complexity: The time complexity depends on whether the queue uses an array, circular array, or linked list.
Frequently Asked Questions
1. What is a simple queue?
A simple queue is a linear data structure that follows the FIFO principle, where the first inserted element is the first one removed.
2. Which principle does a simple queue follow?
A simple queue follows the FIFO (First In, First Out) principle.
3. Where does insertion take place in a simple queue?
Insertion takes place at the rear of the queue.
4. Where does deletion take place in a simple queue?
Deletion takes place at the front of the queue.
5. What is the difference between a simple queue and a circular queue?
A simple queue uses a linear arrangement, while a circular queue connects the last position logically to the first position to reuse available space.
0 Comments