Sai A Sai A
Updated date Jul 21, 2023
In this blog, we will learn different methods to convert a Stack to a Deque in Java and unlock the additional functionality provided by a Deque. This blog provides code examples and explanations for each method, helping you understand the process of converting a Stack to a Deque.

Introduction:

In Java, both Stack and Deque are data structures that allow elements to be added and removed. While a Stack follows the LIFO (Last-In-First-Out) principle, a Deque (Double Ended Queue) allows elements to be added or removed from both ends. There may be scenarios where we need to convert a Stack to a Deque to leverage the additional functionality provided by a Deque. In this blog post, we will explore different methods to convert a Stack to a Deque in Java, along with explanations, code examples, and their corresponding output.

Method 1: Converting a Stack to a Deque using a Constructor

The simplest approach is to create a new Deque object and pass the Stack as an argument to its constructor. Here's the code:

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class StackToDequeConverter {

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);

        Deque<Integer> deque = new ArrayDeque<>(stack);

        System.out.println("Converted Deque: " + deque);
    }
}

Output:

Converted Deque: [3, 2, 1]

In this method, we create a new Stack and add elements to it. Then, we create a new Deque object using the ArrayDeque class and pass the Stack as an argument to its constructor. The elements from the Stack are added to the Deque using this constructor. Finally, we print the converted Deque, which will have the same elements as the original Stack but in reverse order.

Method 2: Converting a Stack to a Deque using the addAll() method

Another way to convert a Stack to a Deque is by using the addAll() method of the Deque class. This method takes a Collection as input and adds all the elements from the given Collection to the Deque. Here's an example:

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class StackToDequeConverter {

    public static void main(String[] args) {
        Stack<String> stack = new Stack<>();
        stack.push("Hello");
        stack.push("World");
        stack.push("!");

        Deque<String> deque = new ArrayDeque<>();
        deque.addAll(stack);

        System.out.println("Converted Deque: " + deque);
    }
}

Output:

Converted Deque: [!, World, Hello]

In this method, we create a new Stack and add elements to it. Then, we create a new empty Deque using the ArrayDeque class. We use the addAll() method to add all the elements from the Stack to the Deque. The elements are added in the same order they were present in the Stack. Finally, we print the converted Deque, which will have the elements in the same order as the original Stack.

Method 3: Converting a Stack to a Deque using push() and pop() operations

This method involves manually popping elements from the Stack and pushing them into the Deque using the push() method. Here's an example:

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;

public class StackToDequeConverter {

    public static void main(String[] args) {
        Stack<Character> stack = new Stack<>();
        stack.push('a');
        stack.push('b');
        stack.push('c');

        Deque<Character> deque = new ArrayDeque<>();
        while (!stack.isEmpty()) {
            deque.push(stack.pop());
        }

        System.out.println("Converted Deque: " + deque);
    }
}

Output:

Converted Deque: [c, b, a]

In this method, we create a new Stack and add elements to it. Then, we create a new empty Deque using the ArrayDeque class. We iterate over the elements in the Stack using a while loop. In each iteration, we pop an element from the Stack and push it into the Deque using the push() method. This way, the elements are added to the Deque in reverse order. Finally, we print the converted Deque, which will have the elements in the reverse order of the original Stack.

Conclusion:

In this blog post, we explored different methods to convert a Stack to a Deque in Java. We learned about using a constructor, the addAll() method, and manual element transfer using push() and pop() operations. 

Comments (0)

There are no comments. Be the first to comment!!!