Martina312

Подели по парност

Feb 3rd, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PodeliParnost {
  4.     public static void podeli(DLL<Integer> list){
  5.         DLL<Integer> evenList=new DLL<>();
  6.         DLL<Integer> oddList=new DLL<>();
  7.  
  8.         DLLNode<Integer> tmp=list.getFirst();
  9.         DLLNode<Integer> tmp2=list.getLast();
  10.  
  11.         int n=list.length()/2;
  12.  
  13.         while(n!=0){
  14.             n--;
  15.             if(tmp.element%2==0){
  16.                 evenList.insertLast(tmp.element);
  17.             }else{
  18.                 oddList.insertLast(tmp.element);
  19.             }
  20.             tmp=tmp.succ;
  21.  
  22.             if(tmp2.element%2==0){
  23.                 evenList.insertLast(tmp2.element);
  24.             }else{
  25.                 oddList.insertLast(tmp2.element);
  26.             }
  27.             tmp2=tmp2.pred;
  28.         }
  29.         if(list.length()%2!=0){
  30.             if(tmp.element%2==0)
  31.                 evenList.insertLast(tmp.element);
  32.             else
  33.                 oddList.insertLast(tmp.element);
  34.         }
  35.         System.out.println(evenList);
  36.         System.out.println(oddList);
  37.     }
  38.     public static void main(String[] args) {
  39.         Scanner in=new Scanner(System.in);
  40.         int n=in.nextInt();
  41.         DLL<Integer> list=new DLL<>();
  42.         for(int i=0;i<n;i++){
  43.             list.insertLast(in.nextInt());
  44.         }
  45.  
  46.         podeli(list);
  47.     }
  48. }
Add Comment
Please, Sign In to add comment