Advertisement
DamSi

Untitled

Nov 6th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package vezbanje_za_prv_kolokvium;
  7.  
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStreamReader;
  11.  
  12. /**
  13.  *
  14.  * @author Damjan
  15.  */
  16. public class PrevrtiLista {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.     public static void main(String[] args) throws IOException {
  22.         DLL<Integer> lista = new DLL<>();
  23.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  24.                 System.in));
  25.         String s = stdin.readLine();
  26.         int N = Integer.parseInt(s);
  27.         s = stdin.readLine();
  28.         String[] pomniza = s.split(" ");
  29.         for (int i = 0; i < N; i++) {
  30.             lista.insertLast(Integer.parseInt(pomniza[i]));
  31.         }
  32.         DLL<Integer> prevrtena = prevrti(lista);
  33.         DLLNode<Integer> jazol = prevrtena.getFirst();
  34.         while (jazol != null) {
  35.             System.out.print(jazol.element);
  36.             if (jazol.succ != null) {
  37.                 System.out.print(" ");
  38.             }
  39.             jazol = jazol.succ;
  40.         }
  41.     }
  42.  
  43.     static DLL<Integer> prevrti(DLL<Integer> lista) {
  44.         DLLNode<Integer> jazol = lista.getLast();
  45.         DLL<Integer> rezultat = new DLL<>();
  46.         while (jazol != null) {
  47.             if (jazol.element % 2 == 0) {
  48.                 rezultat.insertLast(jazol.element);
  49.                 jazol = jazol.pred;
  50.             } else {
  51.                 rezultat.insertLast(jazol.element);
  52.                 jazol = jazol.pred;
  53.             }
  54.  
  55.         }
  56.  
  57.         return rezultat;
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement