-
Notifications
You must be signed in to change notification settings - Fork 33
/
Problem_05.java
52 lines (48 loc) · 1.29 KB
/
Problem_05.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Cracking-The-Coding-Interview
* Problem_05.java
*/
package com.deepak.ctci.Ch03_Stacks_And_Queues;
import java.util.Stack;
/**
* <br> Problem Statement :
*
* Write a program to sort a stack such that the smallest items
* are on top of stack. You can use an additional temporary stack,
* but you may not copy the elements into any other data structure
* (such as array). The stack supports the following operations:
* push, pop, peek and isEmpty.
*
* </br>
*
* @author Deepak
*/
public class Problem_05 {
/**
* Method to sort stack
*
* @param input
* @return {@link Stack}
*/
public static Stack<Integer> sortStack(Stack<Integer> input) {
/* If input is null, no processing needed */
if (input == null) {
return null;
}
/*Create a temp stack */
Stack<Integer> tempStack = new Stack<>();
/* Keep going until input is not empty */
while (!input.isEmpty()) {
/* Pop value from input */
int tempValue = input.pop();
/* We want smallest one at the bottom. So keep comparing and
* if temp stack has bigger item, pop it and push it to input stack */
while (!tempStack.isEmpty() && tempStack.peek() > tempValue) {
input.push(tempStack.pop());
}
/* Push temp value to the temp stack */
tempStack.push(tempValue);
}
return tempStack;
}
}