思路:定义两个队列A,B分别用来存储奇数号的客户和偶数号的客户,然后用Queue.offer()让用户入队,用Queue.poll()让用户出队,用Queue.peek()输出队首元素。
//尽量使用offer()方法添加元素,使用poll()方法移除元素。dd()和remove()方法在失败的时候会抛出异常。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
Queue<Integer> a= new LinkedList<>();;
Queue<Integer> b= new LinkedList<>();;
for(int i=0;i<n;i++)
{
int num = cin.nextInt();
if(num%2==1) a.offer(num); //
else if(num%2==0) b.offer(num);
}
int flag=0;
while(!a.isEmpty() || !b.isEmpty())
{
if(!a.isEmpty()) {
if(flag==0)
{
System.out.print(a.peek());
a.poll();
flag=1;
}
else {
System.out.print(" "+a.peek());
a.poll();
}
}
if(!a.isEmpty()) {
System.out.print(" "+a.peek());
a.poll();
}
if(!b.isEmpty()) {
if(flag==0)
{
System.out.print(b.peek());
b.poll();
flag=1;
}
else {
System.out.print(" "+b.peek());
b.poll();
}
}
}
}
}