Advertisement
Guest User

Untitled

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