Queues have been an integral part of computer science and find their applications in various real-world scenarios like ticket counters, task scheduling, and data buffering. In this blog post, we will learn how to implement Queue using arrays in Java.
Understanding the Queue
A queue is a linear data structure that follows the First In First Out (FIFO) principle. That means the data that comes in first gets processed first, akin to how we stand in lines or "queues" in our daily lives.
Basic Operations:
enqueue: Adds an item to the rear of the queue.
dequeue: Removes and returns the front item from the queue.
isEmpty: Checks if the queue is empty.
isFull: Checks if the queue is full.
front: Returns the front item without removing it.
Java Implementation Using Array
Let's jump into the code:
public class Queue {
private int front, rear, size;
private int capacity;
private int[] array;
public Queue(int capacity) {
this.capacity = capacity;
front = this.size = 0;
rear = capacity - 1;
array = new int[this.capacity];
}
// Enqueue function
public void enqueue(int item) {
if (isFull()) {
System.out.println("Queue is full!");
return;
}
rear = (rear + 1) % capacity;
array[rear] = item;
size++;
}
// Dequeue function
public int dequeue() {
if (isEmpty()) {
System.out.println("Queue is empty!");
return Integer.MIN_VALUE;
}
int item = array[front];
front = (front + 1) % capacity;
size--;
return item;
}
// Check if the queue is empty
public boolean isEmpty() {
return (size == 0);
}
// Check if the queue is full
public boolean isFull() {
return (size == capacity);
}
// Get the front item
public int front() {
if (isEmpty())
return Integer.MIN_VALUE;
return array[front];
}
}
Testing the Queue:
public class Main {
public static void main(String[] args) {
Queue queue = new Queue(5);
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
System.out.println(queue.dequeue() + " dequeued from queue");
System.out.println("Front item is " + queue.front());
queue.enqueue(40);
queue.enqueue(50);
System.out.println(queue.dequeue() + " dequeued from queue");
}
}
Output:
10 dequeued from queue
Front item is 20
20 dequeued from queue
Pros and Cons of Using Array for Queue Implementation
Pros:
Simplicity: Implementing a queue with arrays is straightforward and easy to understand.
Memory Allocation: Arrays in Java have contiguous memory allocation.
Cons:
Fixed Size: The size of the array must be defined at the start, making it less flexible.
Operations Can Be Costly: If not implemented with the circular queue method, operations like enqueue and dequeue can become O(n) operations due to shifting.
Conclusion
Arrays provide a basic yet powerful way to implement queues in Java. They give a clear insight into how data is managed within a queue, making it an excellent starting point for understanding more advanced data structures. In practice, however, developers often use Java's built-in Queue interface and related classes for more flexibility and functionality.
Comments
Post a Comment
Leave Comment