Advertisement
BorjanCrvenkov

Aips lab2 Spoj listi

Oct 31st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 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 elem, SLLNode<E> succ) {
  12. this.element = elem;
  13. this.succ = succ;
  14. }
  15. @Override
  16. public String toString() {
  17. return element.toString();
  18. }
  19. }
  20.  
  21. class SLL<E extends Comparable<E>> {
  22. private SLLNode <E> first;
  23.  
  24. public SLL() {
  25. this.first=null;
  26. }
  27. public SLLNode<E> getFirst() {
  28. return first;
  29. }
  30. public void insertLast(E ob) {
  31. if(first!=null) {
  32. SLLNode<E> temp=first;
  33. while(temp.succ != null) {
  34. temp=temp.succ;
  35. }
  36. SLLNode<E> ins =new SLLNode<E> (ob,null);
  37. temp.succ=ins;
  38. } else {
  39. SLLNode<E> ins = new SLLNode<E>(ob,first);
  40. first = ins;
  41. }
  42.  
  43. }
  44. public void deleteDuplicate(SLL<E> list) {
  45. SLLNode<E> tmp=list.getFirst();
  46. while( tmp!=null && tmp.succ!=null) {
  47. if(tmp.element==tmp.succ.element) {
  48. tmp.succ=tmp.succ.succ;
  49. } else {
  50. tmp=tmp.succ;
  51. }
  52. }
  53. }
  54.  
  55. public SLL<E> joinLists(SLL<E> list2) {
  56. SLL<E> rezultat = new SLL<E>();
  57. SLLNode<E> jazol1=this.getFirst();
  58. SLLNode<E> jazol2 = list2.getFirst();
  59.  
  60. while(jazol1 !=null&&jazol2 != null) {
  61. if(jazol1.element.compareTo(jazol2.element)<0) {
  62. rezultat.insertLast(jazol1.element);
  63. jazol1=jazol1.succ;
  64. } else {
  65. rezultat.insertLast(jazol2.element);
  66. jazol2=jazol2.succ;
  67. }
  68. }
  69. if(jazol1 != null) {
  70. while(jazol1 != null) {
  71. rezultat.insertLast(jazol1.element);
  72. jazol1=jazol1.succ;
  73. }
  74. }
  75.  
  76. if(jazol2 != null) {
  77. while(jazol2 != null) {
  78. rezultat.insertLast(jazol2.element);
  79. jazol2=jazol2.succ;
  80. }
  81. }
  82. return rezultat;
  83. }
  84. @Override
  85. public String toString() {
  86. String print = new String();
  87. if (first != null) {
  88. SLLNode<E> tmp = first;
  89. print += tmp + " ";
  90. while (tmp.succ != null) {
  91. if(tmp.succ == null){
  92. tmp = tmp.succ;
  93. print += tmp;
  94. }else{
  95. tmp = tmp.succ;
  96. print += tmp + " ";
  97. }
  98.  
  99. }
  100. } else
  101. print = "Prazna lista!!!";
  102. return print;
  103. }
  104.  
  105. }
  106.  
  107.  
  108.  
  109. public class SLLJoinLists {
  110.  
  111. public static void main(String[] args) throws IOException {
  112. SLL<Integer> lista1=new SLL<Integer>();
  113. SLL<Integer> lista2=new SLL<Integer>();
  114.  
  115. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  116. String s = stdin.readLine();
  117. int N = Integer.parseInt(s);
  118. s = stdin.readLine();
  119. String[] pomniza = s.split(" ");
  120. for (int i = 0; i < N; i++) {
  121. lista1.insertLast(Integer.parseInt(pomniza[i]));
  122. }
  123.  
  124. s = stdin.readLine();
  125. N = Integer.parseInt(s);
  126. s = stdin.readLine();
  127. pomniza = s.split(" ");
  128. for (int i = 0; i < N; i++) {
  129. lista2.insertLast(Integer.parseInt(pomniza[i]));
  130. }
  131.  
  132. SLL<Integer> spoeni = lista1.joinLists(lista2);
  133. spoeni.deleteDuplicate(spoeni);
  134. System.out.println(spoeni);
  135.  
  136. // spoeni = lista1.joinLists(lista2);
  137. /*Iterator<Integer> it = spoeni.iterator();
  138. while (it.hasNext()) {
  139. System.out.print(it.next());
  140. if(it.hasNext())
  141. System.out.print(" ");
  142. }
  143. System.out.println();*/
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement