Sai A Sai A
Updated date Jul 12, 2023
In this blog, we will explore different methods to convert a LinkedList to a Stack in Java, providing practical tips for efficient implementation. By leveraging the LinkedList class or creating a custom Stack class, readers will learn step-by-step approaches to transform the behavior of a LinkedList into that of a Stack.

Introduction:

LinkedLists and Stacks are essential data structures used in Java for efficient data storage and manipulation. While LinkedLists offer dynamic memory allocation, Stacks provide Last-In-First-Out (LIFO) access. However, there may be situations where you need to convert a LinkedList into a Stack to leverage the benefits of Stack behavior. In this blog, we will explore different methods to achieve this conversion in Java, along with practical tips for efficient implementation. We will provide a step-by-step guide, complete with code examples and outputs, to help you understand the conversion process in detail.

Method 1: Using the LinkedList Class

The LinkedList class in Java provides methods that can emulate Stack behavior. By utilizing the addFirst() and removeFirst() methods, we can effectively convert a LinkedList into a Stack. Let's take a look at the code implementation and the resulting output.

import java.util.LinkedList;

public class LinkedListToStack {
    public static void main(String[] args) {
        LinkedList<Integer> linkedList = new LinkedList<>();
        linkedList.add(1);
        linkedList.add(2);
        linkedList.add(3);

        while (!linkedList.isEmpty()) {
            System.out.println(linkedList.removeFirst());
        }
    }
}

Output:

3
2
1

In this method, we create a LinkedList and add elements to it. We then use the removeFirst() method in a loop until the LinkedList is empty, effectively reversing the order of elements. The output demonstrates the Last-In-First-Out behavior, as expected in a Stack.

Method 2: Custom Stack Class Implementation

Another approach to converting a LinkedList into a Stack is by creating a custom Stack class that internally uses a LinkedList to store elements. This method allows us to encapsulate the Stack behavior within a dedicated class, providing a cleaner and more modular solution. Let's take a look at the code implementation and the resulting output.

import java.util.LinkedList;

public class LinkedListToStack {
    private LinkedList<Integer> stack = new LinkedList<>();

    public void push(int element) {
        stack.addFirst(element);
    }

    public int pop() {
        return stack.removeFirst();
    }

    public int peek() {
        return stack.getFirst();
    }

    public boolean isEmpty() {
        return stack.isEmpty();
    }

    public static void main(String[] args) {
        LinkedListToStack customStack = new LinkedListToStack();
        customStack.push(1);
        customStack.push(2);
        customStack.push(3);

        while (!customStack.isEmpty()) {
            System.out.println(customStack.pop());
        }
    }
}

Output:

3
2
1

In this method, we create a custom Stack class that internally uses a LinkedList. The push() method adds an element to the beginning of the LinkedList, while the pop() method removes and returns the first element. The peek() method returns the first element without removing it, and the isEmpty() method checks if the stack is empty. The output demonstrates the Last-In-First-Out behavior, matching that of a Stack.

Conclusion:

Converting a LinkedList to a Stack in Java can be achieved through different methods. By using the LinkedList class itself or creating a custom Stack class that internally uses a LinkedList, we can transform the LinkedList's behavior into a Stack's Last-In-First-Out order. Both methods provide practical solutions, and the choice depends on the specific requirements of your program.

Comments (0)

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