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<E extends Comparable <E>> {
- public static 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;
- }
- }
- public static 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> find(E o) {
- if (first != null) {
- DLLNode<E> tmp = first;
- while (tmp.element != o&&tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- 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;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null)
- 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 throw Exception
- 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 String toStringR() {
- String ret = new String();
- if (last != null) {
- DLLNode<E> tmp = last;
- ret += tmp + "<->";
- while (tmp.pred != null) {
- tmp = tmp.pred;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- private class LRIterator<E> implements Iterator<E>{
- private DLLNode<E> place;
- private DLLNode<E> prev;
- private DLLNode<E> curr;
- private LRIterator(){
- place=(DLLNode<E>) first;
- curr=null;
- prev=null;
- }
- public boolean hasNext(){
- return (place != null);
- }
- public E next(){
- if(place==null){
- throw new NoSuchElementException();
- }
- if(place==last){
- E nextElem=place.element;
- prev=curr;
- curr=place;
- place=null;
- return nextElem;
- }
- E nextElem=place.element;
- prev=curr;
- curr=place;
- place=place.succ;
- return nextElem;
- }
- @Override
- public void remove() {
- // TODO Auto-generated method stub
- }
- }
- public Iterator<E> iterator(){
- return new LRIterator<E>();
- }
- }
- public static DLL funkcija(DLL lista) {
- DLL rezP = new DLL<>();
- DLLNode jazol1 = lista.getFirst();
- //System.out.println("Razgleduvam"+jazol1.element);
- while (jazol1 != null) {
- Integer tmp;
- tmp=(int)jazol1.element;
- if (tmp % 2 == 0) {
- //System.out.println("Ova e"+jazol1.element);
- rezP.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- //System.out.println("Printam"+rezP);
- }
- else {
- jazol1=jazol1.succ;
- }
- }
- return rezP;
- }
- public static DLL funkcija1(DLL lista) {
- DLL rezN = new DLL<>();
- DLLNode jazol1 = lista.getFirst();
- //System.out.println("Razgleduvam"+jazol1.element);
- while (jazol1 != null) {
- Integer tmp1;
- tmp1=(int)jazol1.element;
- if (tmp1 % 2 != 0) {
- rezN.insertLast(jazol1.element);
- jazol1 = jazol1.succ;
- }
- else
- {
- jazol1=jazol1.succ;
- }
- }
- return rezN;
- }
- public static void main(String[] args) throws IOException {
- DLL lista = new DLL();
- 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 pom=new DLL();
- pom=funkcija1(lista);
- Iterator<Integer> it2 = pom.iterator();
- while (it2.hasNext()) {
- System.out.print(it2.next());
- if(it2.hasNext())
- System.out.print(" ");
- }
- System.out.println();
- DLL pom1=new DLL();
- pom1=funkcija(lista);
- Iterator<Integer> it = pom1.iterator();
- while (it.hasNext()) {
- System.out.print(it.next());
- if(it.hasNext()){
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement