I am looking for a way to change the order of the elements in a Stack, so that the even numbers go after odd numbers.
For example, the following stack:
5, 2, 6, 7, 1, 4, 3
Becomes:
5, 7, 1, 3, 2, 6, 4
Here is my current code. I'm stuck on finding out how to change the order :
public static void main(String[] args) {
Stack<Integer> p = new Stack<>();
p.push(3);
p.push(4);
p.push(1);
p.push(7);
p.push(6);
p.push(2);
p.push(5);
ListIterator<Integer> ListIterator = p.listIterator(p.size());
while (ListIterator.hasPrevious()) {
Integer i = ListIterator.previous();
System.out.println(i);
}
}
