Advertisement
LonelyShepherd

Двојно поврзана листа: Раздели по парност [Лаб. 2.1]

Oct 24th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class DLLNode<E> {
  6.     protected E element;
  7.     protected DLLNode<E> predecessor, successor;
  8.    
  9.     public DLLNode(E element, DLLNode<E> predecessor, DLLNode<E> successor) {
  10.         this.element = element;
  11.         this.predecessor = predecessor;
  12.         this.successor = successor;
  13.     }
  14. }
  15.  
  16. class DLL<E> {
  17.     private DLLNode<E> first, last;
  18.    
  19.     public DLL() {
  20.         first = last = null;
  21.     }
  22.    
  23.     public void insertFirst(E o) {
  24.         DLLNode<E> insert = new DLLNode<E>(o, null, first);
  25.         if(first == null)
  26.             last = insert;
  27.         else first.predecessor = insert;
  28.         first = insert;        
  29.     }
  30.    
  31.     public void insertLast(E o) {
  32.         if(first == null)
  33.             insertFirst(o);
  34.         else {
  35.             DLLNode<E> insert = new DLLNode<E>(o, last, null);
  36.             last.successor = insert;
  37.             last = insert;
  38.         }
  39.     }
  40.    
  41.     public DLLNode<E> getFirst() {
  42.         return first;
  43.     }
  44.    
  45.     public String toString() {
  46.         String s = new String();
  47.         DLLNode<E> tmp = first;
  48.         while(tmp != null) {
  49.             s += tmp.element;
  50.             if(tmp.successor != null) s += " ";
  51.             tmp = tmp.successor;
  52.         }
  53.        
  54.         return s;
  55.     }
  56. }
  57.  
  58. public class DivideOddEven {
  59.    
  60.  
  61.     public static void main(String[] args) throws IOException {
  62.        
  63.         DLL<Integer> lista = new DLL<Integer>();
  64.        
  65.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  66.         String s = stdin.readLine();
  67.         int N = Integer.parseInt(s);
  68.         s = stdin.readLine();
  69.         String[] pomniza = s.split(" ");
  70.         for (int i = 0; i < N; i++) {
  71.             lista.insertLast(Integer.parseInt(pomniza[i]));
  72.         }
  73.        
  74.         DLL<Integer> evenList = new DLL<Integer>();
  75.         DLL<Integer> oddList = new DLL<Integer>();
  76.        
  77.         DLLNode<Integer> listNode = lista.getFirst();
  78.         while(listNode != null) {          
  79.             if(listNode.element % 2 == 0)
  80.                 evenList.insertLast(listNode.element);
  81.             else oddList.insertLast(listNode.element);
  82.            
  83.             listNode = listNode.successor;
  84.         }
  85.        
  86.         System.out.println(oddList.toString());
  87.         System.out.print(evenList.toString());
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement