CC-16 : Reverse order of elements of stack.
Description:
Given a stack with integer elements, reverse the order of elements of the stack and return the same.
Test cases and expected outputs:
| Input Parameters |
Expected outputs |
Stack nodes: 9 <- 14 <- 17 <- 34 <- 21 <- 28 <-
|
Reversed: 28 <- 21 <- 34 <- 17 <- 14 <- 9 <-
|
Stack nodes: 9 <- 14 <- 17 <- 34 <- 21 <- 28 <- 35 <-
|
Reversed: 35 <- 28 <- 21 <- 34 <- 17 <- 14 <- 9 <-
|
Pseudocode:
| Initialize a Queue for processing purposes.
|
| While input stack size is not zero:
Remove the top element of stack and add it to the Queue.
|
| While Queue size is not zero:
Remove the first element of Queue and add it to the stack.
|
| Return the updated stack.
|
Code:
public Deque<Integer> reverse(Deque<Integer> stack) throws Exception{
Queue<Integer> tempQ=new LinkedList<Integer>();
while (stack.size() !=0) {
tempQ.offer(stack.poll());
}
while (tempQ.size() !=0) {
stack.offerFirst((int) tempQ.poll());
}
return stack;
}
Click here to download and run code and test cases !
Below fully running code can be copied and run on Eclipse or other Java IDEs. Refer the classname in code below. If the class name below is "A", save the code below to a file named A.java before running it.
Be sure to try your own test cases to enhance your understanding !
You can also tweak the code to optimize or add enhancements and custom features.
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
public class StackReverse {
public Deque<Integer> reverse(Deque<Integer> stack) throws Exception{
Queue<Integer> tempQ=new LinkedList<Integer>();
while (stack.size() !=0) {
tempQ.offer(stack.poll());
}
while (tempQ.size() !=0) {
stack.offerFirst((int) tempQ.poll());
}
return stack;
}
public static void printQueue(Queue<Integer> queue) throws Exception {
if (queue.size() == 0) { System.out.println("Queue empty"); return; }
System.out.println();
System.out.println("*********************************************************************");
System.out.print("Queue nodes: ");
Iterator itr=queue.iterator();
while (itr.hasNext()) {
System.out.print(itr.next());
System.out.print(" <- ");
}
System.out.println();
System.out.println("*********************************************************************");
System.out.println();
Thread.sleep(2000);
return;
}
public static void main(String[] args) {
/***********************
Test Cases given below:
***********************/
try {
Deque<Integer> queue=new LinkedList<Integer>();
System.out.print("Add to stack");
queue.add(9);
queue.add(14);
queue.add(17);
queue.add(34);
queue.add(21);
queue.add(28);
printQueue(queue);
StackReverse stackReverse=new StackReverse();
System.out.println("Reversed:");
queue=stackReverse.reverse(queue);
printQueue(queue);
queue=new LinkedList<Integer>();
System.out.print("Add to stack");
queue.add(9);
queue.add(14);
queue.add(17);
queue.add(34);
queue.add(21);
queue.add(28);
queue.add(35);
printQueue(queue);
System.out.println("Reversed:");
queue=stackReverse.reverse(queue);
printQueue(queue);
}catch (Exception exception) {
System.out.print("Exception: "+ exception);
exception.printStackTrace();
}
}
}