🎓 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
1. Introduction
A Queue is a First In First Out (FIFO) data structure. It performs operations from two ends, often termed the "front" and "rear". In this guide, we'll showcase how to implement a queue using arrays in Kotlin.
2. Program Overview
The queue will support the following core operations:
1. Enqueue: To add an element to the rear of the queue.
2. Dequeue: To remove and return the front element of the queue.
3. Peek: To view the front element without removing it.
4. Check if the queue is empty.
We will utilize a fixed-size array to hold the queue elements and two variables to monitor the front and rear positions in the queue.
3. Code Program
class Queue(private val size: Int) {
private val array = IntArray(size)
private var front = -1 // Initially, front is -1 as the queue is empty.
private var rear = -1 // Similarly, rear is -1 indicating an empty queue.
// Enqueue operation
fun enqueue(value: Int) {
if (rear < size - 1) {
if (front == -1) front = 0 // If first element, make front point to 0.
rear++
array[rear] = value
} else {
println("Queue Overflow!")
}
}
// Dequeue operation
fun dequeue(): Int? {
return if (front > -1) {
val dequeuedValue = array[front]
front++
if (front > rear) { // If all elements are dequeued, reset front and rear to -1.
front = -1
rear = -1
}
dequeuedValue
} else {
println("Queue Underflow!")
null
}
}
// Peek operation
fun peek(): Int? {
return if (front != -1) {
array[front]
} else {
println("Queue is Empty!")
null
}
}
// Check if the queue is empty
fun isEmpty(): Boolean = front == -1
}
fun main() {
val queue = Queue(5) // Queue of size 5
queue.enqueue(10)
queue.enqueue(20)
queue.enqueue(30)
queue.enqueue(40)
queue.enqueue(50)
println("Front element: ${queue.peek()}")
println("Dequeued element: ${queue.dequeue()}")
println("Is queue empty? ${queue.isEmpty()}")
}
Output:
Front element: 10 Dequeued element: 10 Is queue empty? false
4. Step By Step Explanation
1. Class Declaration & Initialization:
- We introduce a Queue class that accepts a size (for the fixed-size array).
- An array (array) is initialized using the provided size.
- The front and rear variables start at -1, signifying an empty queue.
2. Enqueue Operation:
- Check if there's space remaining in the queue.
- If so, update the rear and store the value. Adjust the front if it's the first item.
- If not, produce an error message ("Queue Overflow").
3. Dequeue Operation:
- If the queue isn't empty, return the front element and adjust the front.
- If after dequeuing, the queue is empty, reset front and rear to -1.
- If the queue is empty from the start, display an error ("Queue Underflow").
4. Peek Operation:
- Return the front element without dequeuing.
- If the queue is vacant, notify the user.
5. isEmpty Function:
- Returns a Boolean value denoting if the queue is empty.
The main function exemplifies how to utilize the queue. The enqueue operation is performed five times, followed by the peek and dequeue operations. The demo concludes by inspecting if the queue is empty using the isEmpty function.
Through this program, learners can grasp the fundamental concepts behind a queue and its standard operations using arrays in Kotlin.
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