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;
- }
- }
- class DLL<E> {
- protected DLLNode<E> first, last;
- public DLL () {
- this.first = null;
- this.last = null;
- }
- public boolean empty(){
- return (first==null&&last==null);
- }
- public void insertFirst(E o) {
- DLLNode<E> nov = new DLLNode<E>(o, null, first);
- if (first == null)
- last = nov;
- else
- first.pred = nov;
- first = nov;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> nov = new DLLNode<E>(o, last, null);
- last.succ = nov;
- last = nov;
- }
- }
- public void pecati(){
- DLLNode<E> tmp = first;
- while(tmp!=null){
- System.out.print(tmp.element);
- if(tmp.succ!=null)
- System.out.print(" ");
- tmp=tmp.succ;
- }
- }
- }
- public class DivideOddEven {
- public static void Divide(DLL<Integer> lista){
- DLLNode<Integer> tmp = lista.first;
- DLL<Integer> parni=new DLL<>();
- DLL<Integer> neparni=new DLL<>();
- while(tmp!=null){
- if(tmp.element%2==0){
- parni.insertLast(tmp.element);
- }
- else
- neparni.insertLast(tmp.element);
- tmp=tmp.succ;
- }
- if(!neparni.empty()){
- neparni.pecati();
- if(!parni.empty()){
- System.out.println();
- parni.pecati();
- }
- }
- else
- parni.pecati();
- }
- public static void main(String[] args) throws IOException {
- BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
- String s = stdin.readLine();
- DLL<Integer> lista=new DLL<>();
- 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]));
- }
- Divide(lista);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement