🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 queuePros 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment