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> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class DLL<E> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- 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 void insertAfter(E o, DLLNode<E> after) {
- if(after==last) {
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if(before == first) {
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- @Override
- public String toString() {
- String s= new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- while (tmp.succ != null) {
- s += tmp + " ";
- tmp = tmp.succ;
- }
- s += tmp;
- }
- return s;
- }
- }
- public class DivideOddEven {
- public static void podelba (DLL <Integer> lista) {
- DLL<Integer> parni=new DLL<Integer>();
- DLL<Integer> neparni=new DLL<Integer>();
- DLLNode<Integer> node=lista.getFirst();
- while(node != null) {
- if(node.element % 2 ==0) {
- parni.insertLast(node.element);
- node=node.succ;
- } else {
- neparni.insertLast(node.element);
- node=node.succ;
- }
- }
- System.out.println(neparni);
- System.out.print(parni);
- }
- public static void main(String[] args) throws IOException {
- DLL<Integer> lista=new DLL<Integer>();
- BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // cita String
- String br = in.readLine(); // zatva sho samo stirng moze
- int lenght=Integer.parseInt(br); // a brojo e int ustvari
- String s = in.readLine(); // cita elementi
- String[] pomniza = s.split(" ");
- for (int i = 0; i < length; i++) {
- lista.insertLast(Integer.parseInt(pomniza[i]));
- }
- podelba(lista);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement