Advertisement
Guest User

Untitled

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