Full Stack Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
Deque in Java is a double-ended queue. It is used to add or remove data elements from either head or tail. Java.util.Dequeue interface is used by deque in Java which is a subtype of the java.util.Queue interface. Let us study the concept in detail.
Below mentioned pointers are going to be the agenda for this article:
Let’s begin!
The Deque is a double-ended queue. It helps in adding and removing data elements from a data structure from either head or tail. It can be used either as a FIFO or a LIFO. Examples of FIFO and LIFO are Queue and Stack respectively.
This is how the working diagrammatically looks like. Moving on, we have several methods included in the deque. Let’s take a look.
Methods used in deque are as follows:
Well, these were a few methods. Let me show you the implementation process through a Java code.
Take a look at the example program below:
import java.util.ArrayList;
import java.util.List;
public class DoubleEndedQueueImpl {
private List<Integer> deque = new ArrayList<Integer>();
public void insertFront(int item){
//add element at the beginning of the queue
System.out.println("element added at front: "+item);
deque.add(0,item);
System.out.println(deque);
}
public void insertRear(int item){
//add element at the end of the queue
System.out.println("element added at rear: "+item);
deque.add(item);
System.out.println(deque);
}
public void removeFront(){
if(deque.isEmpty()){
System.out.println("Deque underflow, unable to remove.");
return;
}
//remove an item from the beginning of the queue
int rem = deque.remove(0);
System.out.println("element removed from front: "+rem);
System.out.println(deque);
}
public void removeRear(){
if(deque.isEmpty()){
System.out.println("Deque underflow, unable to remove.");
return;
}
//remove an item from the beginning of the queue
int rem = deque.remove(deque.size()-1);
System.out.println("element removed from front: "+rem);
System.out.println(deque);
}
public int peakFront(){
//gets the element from the front without removing it
int item = deque.get(0);
System.out.println("Element at first: "+item);
return item;
}
public int peakRear(){
//gets the element from the rear without removing it
int item = deque.get(deque.size()-1);
System.out.println("Element at rear: "+item);
return item;
}
public static void main(String a[]){
DoubleEndedQueueImpl deq = new DoubleEndedQueueImpl();
deq.insertFront(34);
deq.insertRear(45);
deq.removeFront();
deq.removeFront();
deq.removeFront();
deq.insertFront(21);
deq.insertFront(98);
deq.insertRear(5);
deq.insertFront(43);
deq.removeRear();
}
}
Output
adding at front: 34 [34] adding at rear: 45 [34, 45] removed from front: 34 [45] removed from front: 45 [] Deque underflow!! unable to remove. adding at front: 21 [21] adding at front: 98 [98, 21] adding at rear: 5 [98, 21, 5] adding at front: 43 [43, 98, 21, 5] removed from front: 5 [43, 98, 21]
With this, we have reached towards the end of this article. I hope the content explained above added value to your Java knowledge. Keep reading, keep exploring!
Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.
Got a question for us? Please mention it in the comments section of this “Deque in Java” blog and we will get back to you as soon as possible.
edureka.co