[JAVA-6029] reversing a stack (#13122)

* [JAVA-6029] reversing a stack

* [JAVA-6029] un-necessary file changes removed

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar 2022-12-06 21:26:02 +05:30 committed by GitHub
parent 061c30aa08
commit e858d14b86
3 changed files with 135 additions and 0 deletions

View File

@ -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<Integer> inputStack) {
Queue<Integer> queue = new LinkedList<>();
while (!inputStack.isEmpty()) {
queue.add(inputStack.pop());
}
while (!queue.isEmpty()) {
inputStack.add(queue.remove());
}
return inputStack;
}
public Stack reverseStringStack(Stack<String> inputStack) {
Queue<String> queue = new LinkedList<>();
while (!inputStack.isEmpty()) {
queue.add(inputStack.pop());
}
while (!queue.isEmpty()) {
inputStack.add(queue.remove());
}
return inputStack;
}
public Stack reverseEmployeeStack(Stack<Employee> inputStack) {
Queue<Employee> employeeQ = new LinkedList<>();
while (!inputStack.isEmpty()) {
employeeQ.add(inputStack.pop());
}
while (!employeeQ.isEmpty()) {
inputStack.add(employeeQ.remove());
}
return inputStack;
}
}

View File

@ -0,0 +1,29 @@
package com.baeldung.collections.stackreversal;
import java.util.Stack;
public class ReverseStackUsingRecursion {
public Stack<Integer> reverseIntegerStack(Stack<Integer> inputStack) {
reverseStack(inputStack);
return inputStack;
}
private void reverseStack(Stack<Integer> stack) {
if (stack.isEmpty()) {
return;
}
int top = stack.pop();
reverseStack(stack);
insertBottom(stack, top);
}
private void insertBottom(Stack<Integer> stack, int value) {
if (stack.isEmpty()) {
stack.add(value);
} else {
int top = stack.pop();
insertBottom(stack, value);
stack.add(top);
}
}
}

View File

@ -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<Integer> originalStack = generateStackFromGivenList(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).boxed().collect(Collectors.toList()), new Stack<Integer>());
Stack<Integer> reverseList = generateStackFromGivenList(Arrays.stream(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}).boxed().collect(Collectors.toList()), new Stack<Integer>());
Assert.assertEquals(reverseStack.reverseIntegerStack(originalStack), reverseList);
}
@Test
public void whenStringStack_thenReturnReversedStringStack(){
ReverseStackUsingQueue stackReversal = new ReverseStackUsingQueue();
List<String> listOfWords = Arrays.asList(new String[]{"Hello", "I", "am", "reversing", "a", "stack"});
List<String> listOfWordsReversed = new ArrayList<>(listOfWords);
Collections.reverse(listOfWordsReversed);
Stack<String> originalStack = generateStackFromGivenList(listOfWords, new Stack<String>());
Stack<String> reversedStack = generateStackFromGivenList(listOfWordsReversed, new Stack<String>());
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<Employee> employeeList = new ArrayList<>();
employeeList.add(employee1);
employeeList.add(employee2);
employeeList.add(employee3);
List<Employee> employeeReversed = new ArrayList<>(employeeList);
Collections.reverse(employeeReversed);
Stack<Employee> originalStack = generateStackFromGivenList(employeeList, new Stack<Employee>());
Stack<Employee> reverseStack = generateStackFromGivenList(employeeReversed, new Stack<Employee>());
Assert.assertEquals(stackReversal.reverseEmployeeStack(originalStack), reverseStack);
}
@Test
public void givenIntegerStack_whenStackReversed_thenReturnReversedRecursion(){
ReverseStackUsingRecursion reverseStack = new ReverseStackUsingRecursion();
Stack<Integer> originalStack = generateStackFromGivenList(Arrays.stream(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}).boxed().collect(Collectors.toList()), new Stack<Integer>());
Stack<Integer> reversedStack = generateStackFromGivenList(Arrays.stream(new int[]{10, 9, 8, 7, 6, 5, 4, 3, 2, 1}).boxed().collect(Collectors.toList()), new Stack<Integer>());
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;
}
}