Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com;
- import java.util.Stack;
- public class StackReverse {
- public static void reverse(Stack<Integer> stack) {
- Stack<Integer> temporaryStack = new Stack<>();
- //copy all the elements from given stack to temporary stack
- while(stack.size()>0){
- temporaryStack.push(stack.pop());
- }
- while(temporaryStack.size()>0){
- int x = temporaryStack.pop();
- System.out.print(x + " ");
- stack.push(x);
- }
- }
- public static void main(String[] args) {
- Stack<Integer> stack = new Stack<>();
- stack.push(10);
- stack.push(20);
- stack.push(30);
- stack.push(40);
- reverse(stack);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement