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> {
- protected E element;
- protected DLLNode<E> predecessor, successor;
- public DLLNode(E element, DLLNode<E> predecessor, DLLNode<E> successor) {
- this.element = element;
- this.predecessor = predecessor;
- this.successor = successor;
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- first = last = null;
- }
- public void insertFirst(E o) {
- DLLNode<E> insert = new DLLNode<E>(o, null, first);
- if(first == null)
- last = insert;
- else first.predecessor = insert;
- first = insert;
- }
- public void insertLast(E o) {
- if(first == null)
- insertFirst(o);
- else {
- DLLNode<E> insert = new DLLNode<E>(o, last, null);
- last.successor = insert;
- last = insert;
- }
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public String toString() {
- String s = new String();
- DLLNode<E> tmp = first;
- while(tmp != null) {
- s += tmp.element;
- if(tmp.successor != null) s += " ";
- tmp = tmp.successor;
- }
- return s;
- }
- }
- 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]));
- }
- DLL<Integer> evenList = new DLL<Integer>();
- DLL<Integer> oddList = new DLL<Integer>();
- DLLNode<Integer> listNode = lista.getFirst();
- while(listNode != null) {
- if(listNode.element % 2 == 0)
- evenList.insertLast(listNode.element);
- else oddList.insertLast(listNode.element);
- listNode = listNode.successor;
- }
- System.out.println(oddList.toString());
- System.out.print(evenList.toString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement