Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- class DLLNode<E>{
- public E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E element, DLLNode<E> pred, DLLNode<E> succ) {
- super();
- this.element = element;
- this.pred = pred;
- this.succ = succ;
- }
- }
- class DLL<E extends Number>{
- private DLLNode<E> first, last;
- public DLL()
- {
- this.first = null;
- this.last = null;
- }
- public void insertFirst(E o)
- {
- DLLNode<E> ins = new DLLNode<E>(o, first, null);
- if(first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o)
- {
- if(first == null)
- insertFirst(o);
- else
- {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public E deleteFirst()
- {
- if(first.succ==null)
- {
- first=null;
- return null;
- }
- if(first != null)
- {
- DLLNode<E> tmp = first;
- first = first.succ;
- first.pred = null;
- if(first == null) last = null;
- return tmp.element;
- }
- else
- return null;
- }
- public E deleteLast()
- {
- if(first != null)
- {
- if(first.succ == null)
- {
- return deleteFirst();
- }
- else
- {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- else
- return null;
- }
- public E delete(DLLNode<E> node)
- {
- if(node==first)
- {
- deleteFirst();
- return node.element;
- }
- if(node==last)
- {
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- public void pecati()
- {
- while(first!=null)
- {
- if(first.succ!=null)
- System.out.print(first.element+" ");
- else
- System.out.print(first.element);
- first=first.succ;
- }
- }
- public void Method()
- {
- DLL<E> newList = new DLL<E>();
- DLLNode<E> tmp = first;
- if(tmp != null)
- {
- while(tmp != null)
- {
- //System.out.println("1");
- if(isEven((DLLNode<Integer>) tmp)==true)
- {
- newList.insertLast(tmp.element);
- delete(tmp);
- tmp = tmp.succ;
- }
- else
- {
- tmp = tmp.succ;
- }
- }
- /*if(isEven((DLLNode<Integer>) tmp)==true)
- {
- newList.insertLast(tmp.element);
- delete(tmp);
- }*/
- }
- /*if(isEven((DLLNode<Integer>) tmp)==true)
- {
- newList.insertLast(tmp.element);
- this.delete(tmp);
- }*/
- this.pecati();
- System.out.print("\n");
- newList.pecati();
- }
- public boolean isEven(DLLNode<Integer> brojce)
- {
- return((brojce.element)%2==0);
- }
- }
- public class DivideOddEven {
- public static void main(String[] args) throws IOException {
- DLL<Integer>lista = new DLL<Integer>();
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- String[] pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista.insertLast(Integer.parseInt(pomniza[i]));
- }
- lista.Method();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement