How to implement Stack and Queue using ArrayDeque in Java
Last Updated :
24 Aug, 2022
ArrayDeque in Java
The ArrayDeque in Java provides a way to apply resizable-array in addition to the implementation of the Deque interface. It is also known as Array Double Ended Queue or Array Deck. This is a special kind of array that grows and allows users to add or remove an element from both sides of the queue. ArrayDeque class implements Queue and Deque interface which provides it a lot of methods and functionalities.
ArrayDeque Class in JAVAHow to use ArrayDeque
ArrayDeque is a class in Java Programming Language that implements the Queue and Deque interface that further extends the Collection Interface. ArrayDeque has all the methods of Queue and Deque such as add(), remove(), addFirst(), addLast(), removeFirst(), removeLast(), and many others inherited from these interfaces. It is very easy to use ArrayDeque class because of the ease of these methods provided in Java.
Insertion at the front
offerFirst() and addFirst() methods can be used to add element(s) at the front of the ArrayDeque. The main difference in between these two methods is that offerFirst() returns True if the element is added and returns False otherwise whereas addFirst() does not return a value.
Code implementation of the above methods:
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
// Driver Code
public static void main(String[] args)
{
// Creating an ArrayDeque
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
// Adding to front of ArrayDeque
// using addFirst() method
arrayDeque.addFirst(15);
arrayDeque.addFirst(19);
// Printing Arraydeque elements
System.out.println(
"Deque After Inserting using addFirst(): "
+ arrayDeque);
// Adding to front of ArrayDeque
// using offerFirst() method
arrayDeque.offerFirst(17);
arrayDeque.offerFirst(22);
System.out.println(
"Deque After Inserting using offerFirst(): "
+ arrayDeque);
}
}
OutputDeque After Inserting using addFirst(): [19, 15]
Deque After Inserting using offerFirst(): [22, 17, 19, 15]
Insertion at the back
offerLast() and addLast() methods can be used to add element(s) at the back of the ArrayDeque. The main difference in between these two methods is that offerLast() returns True if the element is added and returns False otherwise whereas addLast() does not return a value.
Code implementation of the above methods
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
// Driver Code
public static void main(String[] args)
{
// Creating an ArrayDeque
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
// Adding at the end of ArrayDeque
// using addLast() method
arrayDeque.addLast(15);
arrayDeque.addLast(19);
// Printing ArrayDeque elements
System.out.println(
"Deque After Inserting using addLast(): "
+ arrayDeque);
// Adding at the end of ArrayDeque
// using offerLast() method
arrayDeque.offerLast(17);
arrayDeque.offerLast(22);
System.out.println(
"Deque After Inserting using offerLast(): "
+ arrayDeque);
}
}
OutputDeque After Inserting using addLast(): [15, 19]
Deque After Inserting using offerLast(): [15, 19, 17, 22]
Deletion from the front
pollFirst() and removeFirst() can be used to remove the front element(s) from the ArrayDeque. The difference between these elements is that pollFirst() returns NULL if the ArrayDeque is empty whereas removeFirst() throws NoSuchElementException if the ArrayDeque is empty.
Code implementation for the above methods
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
// Driver Code
public static void main(String[] args)
{
// Creating an ArrayDeque
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
// Adding elements to the arrayDeque
arrayDeque.add(15);
arrayDeque.add(19);
arrayDeque.add(17);
arrayDeque.add(22);
// Printing the elements of ArrayDeque
System.out.println("Deque After Insertion: "
+ arrayDeque);
// Removing element from the front
// using removeFirst() method
Integer i1 = arrayDeque.removeFirst();
System.out.println("Deleted Element: " + i1);
// Printing the ArrayDeque
System.out.println(
"Deque after Deletion using removeFirst(): "
+ arrayDeque);
// Removing element from the front
// using pollFirst() method
Integer i2 = arrayDeque.pollFirst();
System.out.println("Deleted Element: " + i2);
System.out.println(
"Deque after Deletion using pollFirst(): "
+ arrayDeque);
}
}
OutputDeque After Insertion: [15, 19, 17, 22]
Deleted Element: 15
Deque after Deletion using removeFirst(): [19, 17, 22]
Deleted Element: 19
Deque after Deletion using pollFirst(): [17, 22]
Deletion from the end
pollLast() and removeLast() can be used to remove the back element(s) from the ArrayDeque. The difference between these elements is that pollLast() returns NULL if the ArrayDeque is empty whereas removeLast() throws NoSuchElementException if the ArrayDeque is empty.
Code implementation for the above methods
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
// Driver Code
public static void main(String[] args)
{
// Creating an ArrayDeque
Deque<Integer> arrayDeque
= new ArrayDeque<Integer>();
// Adding elements to the arrayDeque
arrayDeque.add(15);
arrayDeque.add(19);
arrayDeque.add(17);
arrayDeque.add(22);
// Printing the elements of ArrayDeque
System.out.println("Deque After Insertion: "
+ arrayDeque);
// Removing element from the end
// using removeLast() method
Integer i1 = arrayDeque.removeLast();
System.out.println("Deleted Element: " + i1);
// Printing the ArrayDeque
System.out.println(
"Deque after Deletion using removeLast(): "
+ arrayDeque);
// Removing element from the end
// using pollLast() method
Integer i2 = arrayDeque.pollLast();
System.out.println("Deleted Element: " + i2);
System.out.println(
"Deque after Deletion using pollLast(): "
+ arrayDeque);
}
}
OutputDeque After Insertion: [15, 19, 17, 22]
Deleted Element: 22
Deque after Deletion using removeLast(): [15, 19, 17]
Deleted Element: 17
Deque after Deletion using pollLast(): [15, 19]
How to implement Stack using ArrayDeque
ArrayDeque can be easily used as a Stack as it provides the option to add and remove elements from both ends. There exists push() and pop() methods that make ArrayDeque favorable to be implemented as a Stack as it mimics the working of a usual Stack.
Code implementation of Stack using ArrayDeque
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
// Driver Code
public static void main(String[] args)
{
// Creating an ArrayDeque to use
// as a Stack
Deque<Integer> stack = new ArrayDeque<Integer>();
// Inserting elements in the Stack
// using push() operation
stack.push(17);
stack.push(19);
stack.push(15);
// Printing the elements
System.out.println("Stack after insertion: "
+ stack);
// Removing elements from the Stack
// using pop() operation
stack.pop();
System.out.println("Stack after deletion: "
+ stack);
stack.pop();
System.out.println("Stack after deletion: "
+ stack);
}
}
OutputStack after insertion: [15, 19, 17]
Stack after deletion: [19, 17]
Stack after deletion: [17]
How to implement Queue using ArrayDeque
ArrayDeque implements the Queue interface so it already has all the functionality provided by the interface. So, ArrayDeque can be used to perform all the normal queue operations using add() and remove() methods.
Code implementation of Queue using ArrayDeque
Java
import java.util.ArrayDeque;
import java.util.Deque;
public class GFG {
// Driver Code
public static void main(String[] args)
{
// Creating a Queue using ArrayDeque
Deque<Integer> queue = new ArrayDeque<Integer>();
// Adding the elements to the Queue
queue.add(15);
queue.add(17);
queue.add(22);
// Printing the elements of the Queue
System.out.println("Queue after insertion: "
+ queue);
// Removing elements from the queue
queue.remove();
System.out.println("Queue after deletion: "
+ queue);
queue.remove();
System.out.println("Queue after deletion: "
+ queue);
}
}
OutputQueue after insertion: [15, 17, 22]
Queue after deletion: [17, 22]
Queue after deletion: [22]
Similar Reads
Implement Stack and Queue using Deque Deque also known as double ended queue, as name suggests is a special kind of queue in which insertions and deletions can be done at the last as well as at the beginning. A link-list representation of deque is such that each node points to the next node as well as the previous node. So that insertio
15 min read
Implement enqueue and dequeue using only two stacks in JavaScript ? In this article, we will implement queue's operations (enqueue and dequeue) using only two stacks (in the form of plain arrays) in JavaScript. Before directly jumping into understanding the problem statement let us understand briefly what exactly a stack is and also what exactly a queue is. A stack
6 min read
C++ Program to Implement Queue using Array A queue is a linear data structure that consists of elements arranged in a sequential order where one end is used to add elements, and another for removing them which results in the FIFO (First-In First-Out) order of operations. In this article, we will learn how to write a program to implement queu
8 min read
C++ Program to Implement Stack using array Stack is the fundamental data structure that can operates the under the Last In, First Out (LIFO) principle. This means that the last element added to the stack is the first one to be removed. Implementing the stack using the array is one of the most straightforward methods in the terms of the both
4 min read
JavaScript program to implement queue using stack A queue is a First In First Out (FIFO) data structure, in which the first element added to the queue is the first one to be removed. The different operations associated with Queue include Enqueue, Dequeue etc. A stack is a Last In, First Out (LIFO) data structure, in which the last element added to
3 min read
JavaScript program to implement stack using queue In this article, we implement a JavaScript program to make a stack using a queue data structure. It provides essential stack methods like push(), pop(), and peek(), isEmpty() operations, utilizing either one or two queues to simulate the behavior of a stack. Examples: Input:push(2)push(3)pop()peek()
4 min read
Stack Implementation using Deque A doubly ended queue or deque allows insertion and deletion at both ends. In a stack, we need to do insertions and deletions at one end only. We can use either end of deque (front or back) to implement a stack, DequeIn the below implementation, we use back (or rear) of stack to do both insertions an
2 min read
Implement Stack using Array Stack is a linear data structure which follows LIFO principle. To implement a stack using an array, initialize an array and treat its end as the stackâs top. Implement push (add to end), pop (remove from end), and peek (check end) operations, handling cases for an empty or full stack.Step-by-step ap
10 min read
Implement Stack using Queues Implement a stack using queues. The stack should support the following operations:Push(x): Push an element onto the stack.Pop(): Pop the element from the top of the stack and return it.A Stack can be implemented using two queues. Let Stack to be implemented be 's' and queues used to implement are 'q
15+ min read
Implementation of Deque using Array - Simple A Deque (Double-Ended Queue) is a data structure that allows insertion and deletion of elements at both ends (front and rear). This flexibility makes it more versatile than a regular queue, where insertion and deletion can only happen at one end. In this article, we will explore how to implement a d
7 min read