I have to write a program which adds integers in a deque such that the odd numbers are added last in the deque and the even numbers are added first. I don't know where to add the if statement for the addition of the numbers and how to make my code work. Sorry if my codes seem to be wrong as this is my first program on deque.
package lesson1;
import java.util.*;
public class MyClass1{
public static void main(String[] args) {
Deque<Integer> d= new LinkedList<Integer>();
d.add(10);
d.add(3);
d.add(5);
d.add(6);
d.add(15);
for(int i=0; i<d.size();i++){
Integer head= d.poll();
if(head%2==1){
d.addLast(head);
}
else{
d.addFirst(head);
}
}
System.out.println(d);
}
}
Aucun commentaire:
Enregistrer un commentaire