Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 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. {
  9. protected E data;
  10. protected SLLNode<E> next;
  11. public SLLNode(E data, SLLNode<E> next)
  12. {
  13. this.data = data;
  14. this.next=next;
  15. }
  16. }
  17. class SLL<E extends Comparable<E>>
  18. {
  19. private SLLNode<E> first;
  20. public SLL()
  21. {
  22. first = null;
  23. }
  24. public SLLNode<E> getFirst()
  25. {
  26. return first;
  27. }
  28. public void insertFirst(E obj)
  29. {
  30. SLLNode<E> ins = new SLLNode<>(obj,first);
  31. first = ins;
  32. }
  33. public void insertLast(E obj)
  34. {
  35. if(first!=null)
  36. {
  37. SLLNode<E> temp = first;
  38. while(temp.next!=null) temp=temp.next;
  39. SLLNode<E> ins = new SLLNode<E>(obj,null);
  40. temp.next=ins;
  41. }
  42. else
  43. {
  44. insertFirst(obj);
  45. }
  46. }
  47. public SLL<E> joinLists(SLL<E> lista2)
  48. {
  49. SLL<E> result = new SLL<>();
  50. SLLNode<E> jazol1 = getFirst();
  51. SLLNode<E> jazol2 = lista2.getFirst();
  52. while(jazol1!=null&&jazol2!=null)
  53. {
  54. if(jazol1.next!=null&&jazol1.data.compareTo(jazol1.next.data)==0)
  55. {
  56. jazol1 = jazol1.next;
  57. }
  58. else if(jazol2.next!=null&&jazol2.data.compareTo(jazol2.next.data)==0)
  59. {
  60. jazol2 = jazol2.next;
  61. }
  62. else if(jazol1.data.compareTo(jazol2.data)==0)
  63. {
  64. jazol2 = jazol2.next;
  65. }
  66. else if(jazol1.data.compareTo(jazol2.data)<0)
  67. {
  68. result.insertLast(jazol1.data);
  69. jazol1 = jazol1.next;
  70. }
  71. else
  72. {
  73. result.insertLast(jazol2.data);
  74. jazol2 = jazol2.next;
  75. }
  76. }
  77. if(jazol1!=null)
  78. {
  79. while(jazol1!=null)
  80. {
  81. if(jazol1.next!=null&&jazol1.data.compareTo(jazol1.next.data)==0)
  82. {
  83. jazol1 =jazol1.next;
  84. }
  85. else
  86. {
  87. result.insertLast(jazol1.data);
  88. jazol1=jazol1.next;
  89. }
  90. }
  91. }
  92. if(jazol2!=null)
  93. {
  94. while(jazol2!=null)
  95. {
  96. if(jazol2.next!=null&&jazol2.data.compareTo(jazol2.next.data)==0)
  97. {
  98. jazol2 =jazol2.next;
  99. }
  100. else
  101. {
  102. result.insertLast(jazol2.data);
  103. jazol2=jazol2.next;
  104. }
  105. }
  106. }
  107. return result;
  108. }
  109. public Iterator<E> iterator () {
  110.  
  111. return new LRIterator<E>();
  112. }
  113.  
  114. private class LRIterator<E> implements Iterator<E> {
  115. private SLLNode<E> place, prev, curr;
  116. private LRIterator() {
  117. place = (SLLNode<E>) first;
  118. curr = prev = null;
  119. }
  120. public boolean hasNext() {
  121. return (place != null);
  122. }
  123. public E next() {
  124. if (place == null)
  125. throw new NoSuchElementException();
  126. E nextElem = place.data;
  127. prev = curr;
  128. curr = place;
  129. place = place.next;
  130. return nextElem;
  131. }
  132. }
  133. }
  134.  
  135. public class SLLJoinLists {
  136. public static void main(String[] args) throws IOException {
  137.  
  138. SLL<Integer> lista1 = new SLL<>();
  139. SLL<Integer> lista2 = new SLL<>();
  140. SLL<Integer> spoeni = new SLL<>();
  141. BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  142. String s = stdin.readLine();
  143. int N = Integer.parseInt(s);
  144. s = stdin.readLine();
  145. String[] pomniza = s.split(" ");
  146. for (int i = 0; i < N; i++) {
  147. lista1.insertLast(Integer.parseInt(pomniza[i]));
  148. }
  149.  
  150. s = stdin.readLine();
  151. N = Integer.parseInt(s);
  152. s = stdin.readLine();
  153. pomniza = s.split(" ");
  154. for (int i = 0; i < N; i++) {
  155. lista2.insertLast(Integer.parseInt(pomniza[i]));
  156. }
  157.  
  158. spoeni = lista1.joinLists(lista2);
  159. Iterator<Integer> it = spoeni.iterator();
  160. while (it.hasNext()) {
  161. System.out.print(it.next());
  162. if(it.hasNext())
  163. System.out.print(" ");
  164. }
  165. System.out.println();
  166. }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement