🎓 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 Deque, or double-ended queue, is a data structure that allows you to add or remove elements from both its start and its end. This hybrid linear structure provides the capabilities of both stacks and queues. Common operations associated with a deque include addFront, addRear, removeFront, removeRear, peekFront, and peekRear.
2. Implementation Steps
1. Define a class named Deque.
2. Initialize an empty list to store the deque elements.
3. Implement the addFront method to add elements to the start of the deque.
4. Implement the addRear method to add elements to the end of the deque.
5. Implement the removeFront method to remove the first element.
6. Implement the removeRear method to remove the last element.
7. Implement the peekFront method to view the first element without removing it.
8. Implement the peekRear method to view the last element without removing it.
9. Implement an isEmpty method to check if the deque is empty.
3. Implementation in Kotlin Programming
class Deque<T> {
private val elements: MutableList<T> = mutableListOf()
// 3. Implement the `addFront` method
fun addFront(item: T) {
elements.add(0, item)
}
// 4. Implement the `addRear` method
fun addRear(item: T) {
elements.add(item)
}
// 5. Implement the `removeFront` method
fun removeFront(): T? {
if (isEmpty()) {
return null
}
return elements.removeAt(0)
}
// 6. Implement the `removeRear` method
fun removeRear(): T? {
if (isEmpty()) {
return null
}
return elements.removeAt(elements.size - 1)
}
// 7. Implement the `peekFront` method
fun peekFront(): T? {
return elements.firstOrNull()
}
// 8. Implement the `peekRear` method
fun peekRear(): T? {
return elements.lastOrNull()
}
// 9. Implement the `isEmpty` method
fun isEmpty() = elements.isEmpty()
}
// Client code to demonstrate the deque functionality
val deque = Deque<Int>()
deque.addRear(1)
deque.addRear(2)
deque.addFront(0)
println(deque.peekFront()) // Expected Output: 0
println(deque.peekRear()) // Expected Output: 2
println(deque.removeFront()) // Expected Output: 0
println(deque.removeRear()) // Expected Output: 2
Output:
0 2 0 2
Explanation:
1. A Deque class is defined with type parameter T to make it generic.
2. Inside the class, a mutable list named elements is initialized. This list holds the deque elements.
3. The addFront method takes an item of type T and adds it to the start of the elements list.
4. The addRear method takes an item and adds it to the end of the elements list.
5. The removeFront method removes and returns the first item from the elements list. If the deque is empty, it returns null.
6. The removeRear method removes and returns the last item from the elements list. If the deque is empty, it returns null.
7. The peekFront method returns the first item from the elements list without removing it. If the deque is empty, it returns null.
8. The peekRear method returns the last item from the elements list without removing it. If the deque is empty, it returns null.
9. The isEmpty method checks if the elements list is empty and returns a boolean value.
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