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> left,right;
- public DLLNode(E e,DLLNode<E> left,DLLNode<E> right){
- element = e;
- this.left = left;
- this.right = right;
- }
- }
- class DLL<E> {
- private DLLNode<E> first,last;
- public DLL(){
- first = null;
- last = null;
- }
- public void insertFirst(E o){
- DLLNode<E> ins = new DLLNode<E>(o,null,first);
- if(first == null)
- last = ins;
- else
- first.left = ins;
- first = ins;
- }
- public void insertLast(E o){
- if(first == null){
- insertFirst(o);
- }
- else{
- DLLNode<E> ins = new DLLNode<E>(o,last,null);
- last.right = ins;
- last = ins;
- }
- }
- public DLLNode<E> getFirst(){
- return first;
- }
- }
- public class DivideOddEven {
- public static void pecati(DLL<Integer> lista){
- DLLNode<Integer> tmp = lista.getFirst();
- while(tmp != null){
- if(tmp.right != null){
- System.out.print(tmp.element+" ");
- }
- else
- System.out.print(tmp.element);
- tmp = tmp.right;
- }
- System.out.println();
- }
- public static DLL<Integer> parni(DLL<Integer> lista){
- DLL<Integer> nova = new DLL<Integer>();
- DLLNode<Integer> tmp = lista.getFirst();
- while(tmp != null){
- if(tmp.element % 2 == 0)
- nova.insertLast(tmp.element);
- tmp = tmp.right;
- }
- return nova;
- }
- public static DLL<Integer> neparni(DLL<Integer> lista){
- DLL<Integer> nova = new DLL<Integer>();
- DLLNode<Integer> tmp = lista.getFirst();
- while(tmp != null){
- if(tmp.element % 2 == 1)
- nova.insertLast(tmp.element);
- tmp = tmp.right;
- }
- return nova;
- }
- 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]));
- }
- pecati(neparni(lista));
- pecati(parni(lista));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement