Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1.  
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class DLLNode<E> {
  8. protected E element;
  9. protected DLLNode<E> pred, succ;
  10.  
  11. public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  12. this.element = elem;
  13. this.pred = pred;
  14. this.succ = succ;
  15. }
  16.  
  17. @Override
  18. public String toString() {
  19. return element.toString();
  20. }
  21. }
  22.  
  23.  
  24. public class DLL<E> {
  25. private DLLNode<E> first, last;
  26.  
  27. public DLL() {
  28. // Construct an empty SLL
  29. this.first = null;
  30. this.last = null;
  31. }
  32.  
  33. public void deleteList() {
  34. first = null;
  35. last = null;
  36. }
  37.  
  38. public int length() {
  39. int ret;
  40. if (first != null) {
  41. DLLNode<E> tmp = first;
  42. ret = 1;
  43. while (tmp.succ != null) {
  44. tmp = tmp.succ;
  45. ret++;
  46. }
  47. return ret;
  48. } else
  49. return 0;
  50.  
  51. }
  52.  
  53. public DLLNode<E> find(E o) {
  54. if (first != null) {
  55. DLLNode<E> tmp = first;
  56. while (tmp.element != o && tmp.succ != null)
  57. tmp = tmp.succ;
  58. if (tmp.element == o) {
  59. return tmp;
  60. } else {
  61. System.out.println("Elementot ne postoi vo listata");
  62. }
  63. } else {
  64. System.out.println("Listata e prazna");
  65. }
  66. return first;
  67. }
  68.  
  69. public void insertFirst(E o) {
  70. DLLNode<E> ins = new DLLNode<E>(o, null, first);
  71. if (first == null)
  72. last = ins;
  73. else
  74. first.pred = ins;
  75. first = ins;
  76. }
  77.  
  78. public void insertLast(E o) {
  79. if (first == null)
  80. insertFirst(o);
  81. else {
  82. DLLNode<E> ins = new DLLNode<E>(o, last, null);
  83. last.succ = ins;
  84. last = ins;
  85. }
  86. }
  87.  
  88. public void insertAfter(E o, DLLNode<E> after) {
  89. if(after==last){
  90. insertLast(o);
  91. return;
  92. }
  93. DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  94. after.succ.pred = ins;
  95. after.succ = ins;
  96. }
  97.  
  98. public void insertBefore(E o, DLLNode<E> before) {
  99. if(before == first){
  100. insertFirst(o);
  101. return;
  102. }
  103. DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  104. before.pred.succ = ins;
  105. before.pred = ins;
  106. }
  107.  
  108. public E deleteFirst() {
  109. if (first != null) {
  110. DLLNode<E> tmp = first;
  111. first = first.succ;
  112. if (first != null) first.pred = null;
  113. if (first == null)
  114. last = null;
  115. return tmp.element;
  116. } else
  117. return null;
  118. }
  119.  
  120. public E deleteLast() {
  121. if (first != null) {
  122. if (first.succ == null)
  123. return deleteFirst();
  124. else {
  125. DLLNode<E> tmp = last;
  126. last = last.pred;
  127. last.succ = null;
  128. return tmp.element;
  129. }
  130. }
  131. // else throw Exception
  132. return null;
  133. }
  134.  
  135. public E delete(DLLNode<E> node) {
  136. if(node==first){
  137. deleteFirst();
  138. return node.element;
  139. }
  140. if(node==last){
  141. deleteLast();
  142. return node.element;
  143. }
  144. node.pred.succ = node.succ;
  145. node.succ.pred = node.pred;
  146. return node.element;
  147.  
  148. }
  149.  
  150. @Override
  151. public String toString() {
  152. String ret = new String();
  153. if (first != null) {
  154. DLLNode<E> tmp = first;
  155. ret += tmp + "<->";
  156. while (tmp.succ != null) {
  157. tmp = tmp.succ;
  158. ret += tmp + "<->";
  159. }
  160. } else
  161. ret = "Prazna lista!!!";
  162. return ret;
  163. }
  164.  
  165. public String toStringR() {
  166. String ret = new String();
  167. if (last != null) {
  168. DLLNode<E> tmp = last;
  169. ret += tmp + "<->";
  170. while (tmp.pred != null) {
  171. tmp = tmp.pred;
  172. ret += tmp + "<->";
  173. }
  174. } else
  175. ret = "Prazna lista!!!";
  176. return ret;
  177. }
  178.  
  179. public DLLNode<E> getFirst() {
  180. return first;
  181. }
  182.  
  183. public DLLNode<E> getLast() {
  184.  
  185. return last;
  186. }
  187.  
  188.  
  189. }
  190.  
  191.  
  192.  
  193. public class DivideOddEven {
  194.  
  195.  
  196. public static void main(String[] args) throws IOException {
  197.  
  198. DLL<Integer> lista = new DLL<>();
  199. DLL<Integer> neparna = new DLL<>();
  200. DLL<Integer> parna = new DLL<>();
  201. DLLNode<Integer> tmp = new DLLNode<>();
  202. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  203. String s = stdin.readLine();
  204. int N = Integer.parseInt(s);
  205. s = stdin.readLine();
  206. String[] pomniza = s.split(" ");
  207. for (int i = 0; i < N; i++) {
  208. lista.insertLast(Integer.parseInt(pomniza[i]));
  209. }
  210. System.out.println(lista.toString());
  211. tmp = lista.getFirst();
  212. while(tmp != null){
  213. if(tmp.element % 2 == 0){
  214. parna.insertLast(tmp.element);
  215. }else{
  216. neparna.insertLast(tmp.element);
  217. }
  218.  
  219. }
  220. System.out.println(parna.toString());
  221.  
  222. System.out.println(neparna.toString());
  223.  
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement