diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/stackreversal/ReverseStackUsingQueue.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/stackreversal/ReverseStackUsingQueue.java new file mode 100644 index 0000000000..6851ed7e88 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/stackreversal/ReverseStackUsingQueue.java @@ -0,0 +1,42 @@ +package com.baeldung.collections.stackreversal; + +import com.baeldung.collections.sorting.Employee; + +import java.util.LinkedList; +import java.util.Queue; +import java.util.Stack; + +public class ReverseStackUsingQueue { + public Stack reverseIntegerStack(Stack inputStack) { + Queue queue = new LinkedList<>(); + while (!inputStack.isEmpty()) { + queue.add(inputStack.pop()); + } + while (!queue.isEmpty()) { + inputStack.add(queue.remove()); + } + return inputStack; + } + + public Stack reverseStringStack(Stack inputStack) { + Queue queue = new LinkedList<>(); + while (!inputStack.isEmpty()) { + queue.add(inputStack.pop()); + } + while (!queue.isEmpty()) { + inputStack.add(queue.remove()); + } + return inputStack; + } + + public Stack reverseEmployeeStack(Stack inputStack) { + Queue employeeQ = new LinkedList<>(); + while (!inputStack.isEmpty()) { + employeeQ.add(inputStack.pop()); + } + while (!employeeQ.isEmpty()) { + inputStack.add(employeeQ.remove()); + } + return inputStack; + } +} diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/stackreversal/ReverseStackUsingRecursion.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/stackreversal/ReverseStackUsingRecursion.java new file mode 100644 index 0000000000..8dbe0c39a6 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/collections/stackreversal/ReverseStackUsingRecursion.java @@ -0,0 +1,29 @@ +package com.baeldung.collections.stackreversal; + +import java.util.Stack; + +public class ReverseStackUsingRecursion { + public Stack reverseIntegerStack(Stack inputStack) { + reverseStack(inputStack); + return inputStack; + } + + private void reverseStack(Stack stack) { + if (stack.isEmpty()) { + return; + } + int top = stack.pop(); + reverseStack(stack); + insertBottom(stack, top); + } + + private void insertBottom(Stack stack, int value) { + if (stack.isEmpty()) { + stack.add(value); + } else { + int top = stack.pop(); + insertBottom(stack, value); + stack.add(top); + } + } +} diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/stackreversal/StackReversalUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/stackreversal/StackReversalUnitTest.java new file mode 100644 index 0000000000..7ad0b71028 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/stackreversal/StackReversalUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.stackreversal; + +import com.baeldung.collections.sorting.Employee; +import com.baeldung.collections.stackreversal.ReverseStackUsingQueue; +import com.baeldung.collections.stackreversal.ReverseStackUsingRecursion; +import org.junit.Assert; +import org.junit.Test; + +import java.util.*; +import java.util.stream.Collectors; + +public class StackReversalUnitTest { + @Test + public void whenIntegerStack_thenReturnReversedIntegerStack(){ + ReverseStackUsingQueue reverseStack = new ReverseStackUsingQueue(); + Stack originalStack = generateStackFromGivenList(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).boxed().collect(Collectors.toList()), new Stack()); + Stack reverseList = generateStackFromGivenList(Arrays.stream(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}).boxed().collect(Collectors.toList()), new Stack()); + Assert.assertEquals(reverseStack.reverseIntegerStack(originalStack), reverseList); + } + + @Test + public void whenStringStack_thenReturnReversedStringStack(){ + ReverseStackUsingQueue stackReversal = new ReverseStackUsingQueue(); + List listOfWords = Arrays.asList(new String[]{"Hello", "I", "am", "reversing", "a", "stack"}); + List listOfWordsReversed = new ArrayList<>(listOfWords); + Collections.reverse(listOfWordsReversed); + Stack originalStack = generateStackFromGivenList(listOfWords, new Stack()); + Stack reversedStack = generateStackFromGivenList(listOfWordsReversed, new Stack()); + Assert.assertEquals(stackReversal.reverseStringStack(originalStack), reversedStack); + } + + @Test + public void whenEmployeeStack_thenReturnReversedEmployeeStack(){ + ReverseStackUsingQueue stackReversal = new ReverseStackUsingQueue(); + Employee employee1 = new Employee("John Doe", new Date()); + Employee employee2 = new Employee("John Nash", new Date()); + Employee employee3 = new Employee("Ryan Howard", new Date()); + List employeeList = new ArrayList<>(); + employeeList.add(employee1); + employeeList.add(employee2); + employeeList.add(employee3); + List employeeReversed = new ArrayList<>(employeeList); + Collections.reverse(employeeReversed); + Stack originalStack = generateStackFromGivenList(employeeList, new Stack()); + Stack reverseStack = generateStackFromGivenList(employeeReversed, new Stack()); + Assert.assertEquals(stackReversal.reverseEmployeeStack(originalStack), reverseStack); + } + + @Test + public void givenIntegerStack_whenStackReversed_thenReturnReversedRecursion(){ + ReverseStackUsingRecursion reverseStack = new ReverseStackUsingRecursion(); + Stack originalStack = generateStackFromGivenList(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).boxed().collect(Collectors.toList()), new Stack()); + Stack reversedStack = generateStackFromGivenList(Arrays.stream(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}).boxed().collect(Collectors.toList()), new Stack()); + Assert.assertEquals(reverseStack.reverseIntegerStack(originalStack), reversedStack); + } + + private Stack generateStackFromGivenList(List elements, Stack stack){ + int start = 0; + while (start < elements.size()){ + stack.add(elements.get(start++)); + } + return stack; + } +}