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;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- public class DivideOddEven {
- public class LLNode<E> {
- LLNode(E data, LLNode<E> prev, LLNode<E> next) {
- this.data = data;
- this.next = next;
- this.prev = prev;
- }
- LLNode<E> next, prev;
- E data;
- }
- public class LinkedList<E> {
- LLNode<E> first, last;
- LinkedList() {
- first = last = null;
- }
- public LLIterator<E> begin() {
- return new LLIterator<E>(first);
- }
- public LLIterator<E> end() {
- return new LLIterator<E>(last);
- }
- public LLNode<E> find(E el) {
- LLIterator<E> it = begin();
- while (it.hasNext()) {
- if (it.next() == el)
- return it.current;
- }
- return null;
- }
- public class LLIterator<E> implements Iterator<E> {
- LLNode<E> place, current;
- LLIterator(LLNode<E> place) {
- this.place = place;
- current = null;
- }
- @Override
- public boolean hasNext() {
- return place != null ;
- }
- public boolean hasPrevious() {
- return place != null;
- }
- @Override
- public E next() {
- if (!hasNext())
- throw new NoSuchElementException();
- E nextEl = place.data;
- current = place;
- place = place.next;
- return nextEl;
- }
- public E previous() {
- if (!hasPrevious())
- throw new NoSuchElementException();
- E nextEl = place.data;
- current = place;
- place = place.prev;
- return nextEl;
- }
- @Override
- public void remove() {
- LLNode<E> node = current;
- if (node != null) {
- LLNode<E> n = node.next;
- LLNode<E> p = node.prev;
- if (n != null)
- n.prev = p;
- if (p != null)
- p.next = n;
- }
- node.prev = null;
- node.next = null;
- if (node == first)
- first = null;
- }
- }
- public void remove(E element) {
- LLNode<E> node = find(element);
- if (node != null) {
- //System.out.println("Removing " + node.data);
- LLNode<E> n = node.next;
- LLNode<E> p = node.prev;
- if (n != null)
- n.prev = p;
- if (p != null)
- p.next = n;
- }
- }
- public void insertFirst(E element) {
- LLNode<E> insertion = new LLNode<E>(element, null, first);
- if (first == null)
- last = first = insertion;
- else
- first.prev = insertion;
- first = insertion;
- }
- public void insertLast(E element) {
- if (first == null)
- insertFirst(element);
- else {
- LLNode<E> insertion = new LLNode<E>(element, last, null);
- if (last != null)
- last.next = insertion;
- last = insertion;
- }
- }
- }
- public static void main(String[] args) throws IOException {
- BufferedReader stdin = new BufferedReader(new InputStreamReader(
- System.in));
- String s = stdin.readLine();
- int N = Integer.parseInt(s);
- s = stdin.readLine();
- DivideOddEven div = new DivideOddEven();
- LinkedList<Integer> lista = div.new LinkedList<Integer>();
- String[] pomniza = s.split(" ");
- for (int i = 0; i < N; i++) {
- lista.insertLast(Integer.parseInt(pomniza[i]));
- }
- Iterator it = lista.begin();
- LinkedList<Integer> parni = div.new LinkedList<Integer>();
- while(it.hasNext())
- {
- int val = (int) it.next();
- if ((val % 2) == 0)
- {
- //lista.remove(val);
- it.remove();
- parni.insertLast(val);
- }
- }
- it = lista.begin();
- boolean imaNeparni = false;
- while (it.hasNext())
- {
- int v = (int)it.next();
- System.out.print(v + ((it.hasNext()) ? " " : ""));
- imaNeparni = true;
- }
- if (imaNeparni) System.out.print("\n");
- it = parni.begin();
- while (it.hasNext())
- {
- int v = (int)it.next();
- System.out.print(v + ((it.hasNext()) ? " " : ""));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement