kacci97

Спој листи наизменично

Oct 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class SLLNode<E>
  6. {
  7. E element;
  8. SLLNode<E> succ;
  9.  
  10. SLLNode(){}
  11. SLLNode(E element, SLLNode<E> succ)
  12. {
  13. this.element=element;
  14. this.succ=succ;
  15. }
  16. }
  17. class SLL<E>
  18. {
  19. SLLNode<E> first;
  20. SLL()
  21. {
  22. first=null;
  23. }
  24. public void insertFirst(E element)
  25. {
  26. SLLNode<E> nov = new SLLNode<>(element,first);
  27. first = nov;
  28. }
  29. public void insertLast(E element)
  30. {
  31. if(first==null) insertFirst(element);
  32. else
  33. {
  34. SLLNode<E> dvizi = first;
  35. while(dvizi.succ != null)
  36. {
  37. dvizi = dvizi.succ;
  38. }
  39. SLLNode<E> last = new SLLNode<>(element,null);
  40. dvizi.succ=last;
  41. }
  42. }
  43. public void insertAfter(E element, SLLNode<E>node)
  44. {
  45. if(node!=null){
  46. SLLNode<E> nov = new SLLNode<>(element,node.succ);
  47. node.succ = nov;
  48. }
  49. }
  50.  
  51. public int getLength()
  52. {
  53. SLLNode<E> dvizi = first;
  54. int length = 0;
  55. while(dvizi!=null)
  56. {
  57. length++;
  58. dvizi = dvizi.succ;
  59. }
  60. return length;
  61. }
  62.  
  63.  
  64. public String toString()
  65. {
  66. String s = new String();
  67. SLLNode<E> dvizi = first;
  68. while(dvizi!=null)
  69. {
  70. s = s+dvizi.element+" ";
  71. dvizi=dvizi.succ;
  72. }
  73. return s;
  74. }
  75. public SLLNode<E> getFirst()
  76. {
  77. return first;
  78. }
  79.  
  80. }
  81.  
  82. public class SpecialSLLJoin {
  83.  
  84. private static SLL<Integer> specialJoin(SLL<Integer> lista1,
  85. SLL<Integer> lista2) {
  86.  
  87. SLL<Integer> spoenaLista = new SLL<>();
  88. SLLNode<Integer> firstL1 = lista1.getFirst();
  89. SLLNode<Integer> firstL2 = lista2.getFirst();
  90. int br1,br2;
  91.  
  92. while((firstL1 != null)||(firstL2!=null))
  93. {
  94. br1=0;br2=0;
  95. while((firstL1!=null)&&(br1!=2))
  96. {
  97. spoenaLista.insertLast(firstL1.element);
  98. firstL1 = firstL1.succ;
  99. br1++;
  100. }
  101. while((firstL2!=null)&&(br2!=2))
  102. {
  103. spoenaLista.insertLast(firstL2.element);
  104. firstL2 = firstL2.succ;
  105. br2++;
  106. }
  107. }
  108. return spoenaLista;
  109. }
  110.  
  111.  
  112. public static void main(String[] args) throws IOException{
  113.  
  114. SLL<Integer> lista1 = new SLL<>();
  115. SLL<Integer> lista2 = new SLL<>();
  116.  
  117. BufferedReader stdin = new BufferedReader(new InputStreamReader(
  118. System.in));
  119. String s = stdin.readLine();
  120. int N = Integer.parseInt(s);
  121. s = stdin.readLine();
  122. String[] pomniza = s.split(" ");
  123. for (int i = 0; i < N; i++) {
  124. lista1.insertLast(Integer.parseInt(pomniza[i]));
  125. }
  126.  
  127. s = stdin.readLine();
  128. N = Integer.parseInt(s);
  129. s = stdin.readLine();
  130. pomniza = s.split(" ");
  131. for (int i = 0; i < N; i++) {
  132. lista2.insertLast(Integer.parseInt(pomniza[i]));
  133. }
  134.  
  135. SLL<Integer> spoeni = specialJoin(lista1,lista2);
  136. System.out.println(spoeni);
  137.  
  138. }
  139.  
  140.  
  141. }
Add Comment
Please, Sign In to add comment