Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class DLLNode<E> {
  6. protected E element;
  7. protected DLLNode<E> pred, succ;
  8. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  9. this.element = elem;
  10. this.pred = pred;
  11. this.succ = succ;
  12. }
  13. }
  14. class DLL<E> {
  15. protected DLLNode<E> first, last;
  16. public DLL () {
  17. this.first = null;
  18. this.last = null;
  19. }
  20. public boolean empty(){
  21. return (first==null&&last==null);
  22. }
  23. public void insertFirst(E o) {
  24. DLLNode<E> nov = new DLLNode<E>(o, null, first);
  25. if (first == null)
  26. last = nov;
  27. else
  28. first.pred = nov;
  29. first = nov;
  30. }
  31. public void insertLast(E o) {
  32. if (first == null)
  33. insertFirst(o);
  34. else {
  35. DLLNode<E> nov = new DLLNode<E>(o, last, null);
  36. last.succ = nov;
  37. last = nov;
  38. }
  39. }
  40. public void pecati(){
  41. DLLNode<E> tmp = first;
  42. while(tmp!=null){
  43. System.out.print(tmp.element);
  44. if(tmp.succ!=null)
  45. System.out.print(" ");
  46. tmp=tmp.succ;
  47. }
  48. }
  49.  
  50. }
  51.  
  52.  
  53. public class DivideOddEven {
  54.  
  55. public static void Divide(DLL<Integer> lista){
  56. DLLNode<Integer> tmp = lista.first;
  57. DLL<Integer> parni=new DLL<>();
  58. DLL<Integer> neparni=new DLL<>();
  59. while(tmp!=null){
  60. if(tmp.element%2==0){
  61. parni.insertLast(tmp.element);
  62. }
  63. else
  64. neparni.insertLast(tmp.element);
  65. tmp=tmp.succ;
  66. }
  67. if(!neparni.empty()){
  68. neparni.pecati();
  69. if(!parni.empty()){
  70. System.out.println();
  71. parni.pecati();
  72. }
  73. }
  74. else
  75. parni.pecati();
  76. }
  77.  
  78. public static void main(String[] args) throws IOException {
  79.  
  80. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  81. String s = stdin.readLine();
  82. DLL<Integer> lista=new DLL<>();
  83. int N = Integer.parseInt(s);
  84. s = stdin.readLine();
  85. String[] pomniza = s.split(" ");
  86. for (int i = 0; i < N; i++) {
  87. lista.insertLast(Integer.parseInt(pomniza[i]));
  88. }
  89. Divide(lista);
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement