Sort the elements in a stack - Java
public class Sort {
public Stack sort(Stack s){
Stack sorted = new Stack();
while (!s.isEmpty()) {
int tmp = s.pop();
while(!sorted.isEmpty() && sorted.pop() > tmp){
s.push(sorted.pop());
}
sorted.push(tmp);
System.out.println(sorted.TOP);
}
return sorted;
}
}
public Stack sort(Stack s){
Stack sorted = new Stack();
while (!s.isEmpty()) {
int tmp = s.pop();
while(!sorted.isEmpty() && sorted.pop() > tmp){
s.push(sorted.pop());
}
sorted.push(tmp);
System.out.println(sorted.TOP);
}
return sorted;
}
}
Comments
Post a Comment