DamSi

lab1 -zadaca 2

Nov 8th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 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. /**
  8. *
  9. * @author Damjan
  10. */
  11. public class SLLJoinLists {
  12.  
  13. public static void main(String[] args) throws IOException {
  14. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  15. String s = stdin.readLine();
  16. int N = Integer.parseInt(s);
  17. s = stdin.readLine();
  18. String[] pomniza = s.split(" ");
  19. SLL<Integer> lista1 = new SLL<>();
  20. SLL<Integer> lista2 = new SLL<>();
  21. SLL<Integer> spoeni;
  22. for (int i = 0; i < N; i++) {
  23. lista1.insertLast(Integer.parseInt(pomniza[i]));
  24. }
  25.  
  26. s = stdin.readLine();
  27. N = Integer.parseInt(s);
  28. s = stdin.readLine();
  29. pomniza = s.split(" ");
  30. for (int i = 0; i < N; i++) {
  31. lista2.insertLast(Integer.parseInt(pomniza[i]));
  32. }
  33.  
  34. spoeni = lista1.joinLists(lista2);
  35. Iterator<Integer> it = spoeni.iterator();
  36. while (it.hasNext()) {
  37. System.out.print(it.next());
  38. if (it.hasNext()) {
  39. System.out.print(" ");
  40. }
  41. }
  42. System.out.println();
  43. }
  44. }
  45.  
  46. class SLLNode<E extends Comparable<E>> {
  47.  
  48. protected E element;
  49. protected SLLNode<E> succ;
  50.  
  51. public SLLNode(E elem, SLLNode<E> succ) {
  52. this.element = elem;
  53. this.succ = succ;
  54. }
  55.  
  56. @Override
  57. public String toString() {
  58. return element.toString();
  59. }
  60. }
  61.  
  62. class SLL<E extends Comparable<E>> {
  63.  
  64. private SLLNode<E> first;
  65.  
  66. public SLL() {
  67. // Construct an empty SLL
  68. this.first = null;
  69. }
  70.  
  71. public void deleteList() {
  72. first = null;
  73. }
  74.  
  75. public int length() {
  76. int ret;
  77. if (first != null) {
  78. SLLNode<E> tmp = first;
  79. ret = 1;
  80. while (tmp.succ != null) {
  81. tmp = tmp.succ;
  82. ret++;
  83. }
  84. return ret;
  85. } else {
  86. return 0;
  87. }
  88.  
  89. }
  90.  
  91. @Override
  92. public String toString() {
  93. String ret = new String();
  94. if (first != null) {
  95. SLLNode<E> tmp = first;
  96. ret += tmp + "->";
  97. while (tmp.succ != null) {
  98. tmp = tmp.succ;
  99. ret += tmp + "->";
  100. }
  101. } else {
  102. ret = "Prazna lista!!!";
  103. }
  104. return ret;
  105. }
  106.  
  107. public void insertFirst(E o) {
  108. SLLNode<E> ins = new SLLNode<E>(o, first);
  109. first = ins;
  110. }
  111.  
  112. public void insertAfter(E o, SLLNode<E> node) {
  113. if (node != null) {
  114. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  115. node.succ = ins;
  116. } else {
  117. System.out.println("Dadenot jazol e null");
  118. }
  119. }
  120.  
  121. public void insertBefore(E o, SLLNode<E> before) {
  122.  
  123. if (first != null) {
  124. SLLNode<E> tmp = first;
  125. if (first == before) {
  126. this.insertFirst(o);
  127. return;
  128. }
  129. //ako first!=before
  130. while (tmp.succ != before) {
  131. tmp = tmp.succ;
  132. }
  133. if (tmp.succ == before) {
  134. SLLNode<E> ins = new SLLNode<E>(o, before);
  135. tmp.succ = ins;
  136. } else {
  137. System.out.println("Elementot ne postoi vo listata");
  138. }
  139. } else {
  140. System.out.println("Listata e prazna");
  141. }
  142. }
  143.  
  144. public void insertLast(E o) {
  145. if (first != null) {
  146. SLLNode<E> tmp = first;
  147. while (tmp.succ != null) {
  148. tmp = tmp.succ;
  149. }
  150. SLLNode<E> ins = new SLLNode<E>(o, null);
  151. tmp.succ = ins;
  152. } else {
  153. insertFirst(o);
  154. }
  155. }
  156.  
  157. public E deleteFirst() {
  158. if (first != null) {
  159. SLLNode<E> tmp = first;
  160. first = first.succ;
  161. return tmp.element;
  162. } else {
  163. System.out.println("Listata e prazna");
  164. return null;
  165. }
  166. }
  167.  
  168. public E delete(SLLNode<E> node) {
  169. if (first != null) {
  170. SLLNode<E> tmp = first;
  171. if (first == node) {
  172. return this.deleteFirst();
  173. }
  174. while (tmp.succ != node&&tmp.succ.succ != null) {
  175. tmp = tmp.succ;
  176. }
  177. if (tmp.succ == node) {
  178. tmp.succ = tmp.succ.succ;
  179. return node.element;
  180. } else {
  181. System.out.println("Elementot ne postoi vo listata");
  182. return null;
  183. }
  184. } else {
  185. System.out.println("Listata e prazna");
  186. return null;
  187. }
  188.  
  189. }
  190.  
  191. public SLLNode<E> getFirst() {
  192. return first;
  193. }
  194.  
  195. public SLLNode<E> find(E o) {
  196. if (first != null) {
  197. SLLNode<E> tmp = first;
  198. while (tmp.element != o&&tmp.succ != null) {
  199. tmp = tmp.succ;
  200. }
  201. if (tmp.element == o) {
  202. return tmp;
  203. } else {
  204. System.out.println("Elementot ne postoi vo listata");
  205. }
  206. } else {
  207. System.out.println("Listata e prazna");
  208. }
  209. return first;
  210. }
  211.  
  212. public Iterator<E> iterator() {
  213. // Return an iterator that visits all elements of this list, in left-to-right order.
  214. return new LRIterator<E>();
  215. }
  216.  
  217. // //////////Inner class ////////////
  218. private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
  219.  
  220. private SLLNode<E> place, curr;
  221.  
  222. private LRIterator() {
  223. place = (SLLNode<E>) first;
  224. curr = null;
  225. }
  226.  
  227. public boolean hasNext() {
  228. return (place != null);
  229. }
  230.  
  231. public E next() {
  232. if (place == null) {
  233. throw new NoSuchElementException();
  234. }
  235. E nextElem = place.element;
  236. curr = place;
  237. place = place.succ;
  238. return nextElem;
  239. }
  240.  
  241. public void remove() {
  242. //Not implemented
  243. }
  244. }
  245.  
  246. public void mirror() {
  247. if (first != null) {
  248. //m=nextsucc, p=tmp,q=next
  249. SLLNode<E> tmp = first;
  250. SLLNode<E> newsucc = null;
  251. SLLNode<E> next;
  252.  
  253. while (tmp != null) {
  254. next = tmp.succ;
  255. tmp.succ = newsucc;
  256. newsucc = tmp;
  257. tmp = next;
  258. }
  259. first = newsucc;
  260. }
  261.  
  262. }
  263.  
  264. public SLL<E> joinLists(SLL<E> lista) {
  265. SLL<E> rezultat = new SLL<>();
  266. SLL<E> bezDuplikati = new SLL<>();
  267. SLLNode<E> jazol1 = this.getFirst();
  268. SLLNode<E> jazol2 = lista.getFirst();
  269.  
  270. while ((jazol1 != null) && (jazol2 != null)) {
  271. if ((jazol1.element.compareTo(jazol2.element)<0)) {
  272. rezultat.insertLast(jazol1.element);
  273. jazol1 = jazol1.succ;
  274. } else {
  275. rezultat.insertLast(jazol2.element);
  276. jazol2 = jazol2.succ;
  277. }
  278. }
  279. if (jazol1 != null) {
  280. while (jazol1 != null) {
  281. rezultat.insertLast(jazol1.element);
  282. jazol1 = jazol1.succ;
  283. }
  284. }
  285. if (jazol2 != null) {
  286. while (jazol2 != null) {
  287. rezultat.insertLast(jazol2.element);
  288. jazol2 = jazol2.succ;
  289. }
  290. }
  291.  
  292. SLLNode<E> jazelZaDupli = rezultat.getFirst();
  293. SLLNode<E> jazelZaDuplikatSledbenik = jazelZaDupli.succ;
  294. while (jazelZaDuplikatSledbenik != null) {
  295. if (jazelZaDupli.element.compareTo(jazelZaDuplikatSledbenik.element)==0) {
  296. jazelZaDupli = jazelZaDupli.succ;
  297. jazelZaDuplikatSledbenik = jazelZaDuplikatSledbenik.succ;
  298. } else {
  299. bezDuplikati.insertLast(jazelZaDupli.element);
  300. jazelZaDupli = jazelZaDupli.succ;
  301. jazelZaDuplikatSledbenik = jazelZaDuplikatSledbenik.succ;
  302. }
  303. }
  304.  
  305. bezDuplikati.insertLast(jazelZaDupli.element);
  306. return bezDuplikati;
  307. }
  308.  
  309. public void merge(SLL<E> in) {
  310. if (first != null) {
  311. SLLNode<E> tmp = first;
  312. while (tmp.succ != null) {
  313. tmp = tmp.succ;
  314. }
  315. tmp.succ = in.getFirst();
  316. } else {
  317. first = in.getFirst();
  318. }
  319. }
  320.  
  321. }
Advertisement
Add Comment
Please, Sign In to add comment