Advertisement
IlijaTrnkovski

lista

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