Advertisement
BorjanCrvenkov

Aips lab2 Spoj listi naizmenicno

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