Advertisement
Guest User

Untitled

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