Created
March 14, 2017 12:29
-
-
Save hrishikesh-mishra/5a9ef82ac7aba14d266adffcd33b3d8f to your computer and use it in GitHub Desktop.
Given a stack how to reverse the contents of stacks using only stack operation (push and pop)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.hrishikesh.ns.stack; | |
| /** | |
| * Problem: | |
| * Given a stack how to reverse the contents of stacks using only | |
| * stack operation (push and pop) | |
| * | |
| * @author hrishikesh.mishra | |
| * @link http://hrishikeshmishra.com/reverse-stack-content-only-using-push-and-pop/ | |
| */ | |
| public class StackReverser { | |
| public <E> void reverse(Stack<E> stack) { | |
| if (stack.isEmpty()) return; | |
| E data = stack.pop(); | |
| reverse(stack); | |
| insertAtBottom(stack, data); | |
| } | |
| private <E> void insertAtBottom(Stack<E> stack, E data) { | |
| if (stack.isEmpty()) { | |
| stack.push(data); | |
| return; | |
| } | |
| E temp = stack.pop(); | |
| insertAtBottom(stack, data); | |
| stack.push(temp); | |
| } | |
| } | |
| class StackReverserTest { | |
| public static void main(String[] args) { | |
| Stack<Integer> stack = new LinkedStack<>(); | |
| stack.push(10); | |
| stack.push(20); | |
| stack.push(30); | |
| stack.push(40); | |
| stack.push(50); | |
| System.out.println("Before reverse: " + stack); | |
| StackReverser reverser = new StackReverser(); | |
| reverser.reverse(stack); | |
| System.out.println("After reverse:" + stack); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment