Advertisement
Guest User

import java.io.BufferedReader; import java.io.IOException; i

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