Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.22 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.Iterator;
  6. import java.util.NoSuchElementException;
  7. import java.util.Iterator;
  8. import java.util.NoSuchElementException;
  9.  
  10. class SLLNode<E> {
  11. protected E element;
  12. protected SLLNode<E> succ;
  13.  
  14. public SLLNode(E elem, SLLNode<E> succ) {
  15. this.element = elem;
  16. this.succ = succ;
  17. }
  18.  
  19. @Override
  20. public String toString() {
  21. return element.toString();
  22. }
  23. }
  24.  
  25. class SLL<E extends Comparable<E>>{
  26. private SLLNode<E> first;
  27.  
  28. public SLL() {
  29. // Construct an empty SLL
  30. this.first = null;
  31. }
  32.  
  33. public void deleteList() {
  34. first = null;
  35. }
  36.  
  37. public int length() {
  38. int ret;
  39. if (first != null) {
  40. SLLNode<E> tmp = first;
  41. ret = 1;
  42. while (tmp.succ != null) {
  43. tmp = tmp.succ;
  44. ret++;
  45. }
  46. return ret;
  47. } else
  48. return 0;
  49.  
  50. }
  51.  
  52. @Override
  53. public String toString() {
  54. String ret = new String();
  55. if (first != null) {
  56. SLLNode<E> tmp = first;
  57. ret += tmp + "->";
  58. while (tmp.succ != null) {
  59. tmp = tmp.succ;
  60. ret += tmp + "->";
  61. }
  62. } else
  63. ret = "Prazna lista!!!";
  64. return ret;
  65. }
  66.  
  67. public void insertFirst(E o) {
  68. SLLNode<E> ins = new SLLNode<E>(o, first);
  69. first = ins;
  70. }
  71.  
  72. public void insertAfter(E o, SLLNode<E> node) {
  73. if (node != null) {
  74. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  75. node.succ = ins;
  76. } else {
  77. System.out.println("Dadenot jazol e null");
  78. }
  79. }
  80.  
  81. public void insertBefore(E o, SLLNode<E> before) {
  82.  
  83. if (first != null) {
  84. SLLNode<E> tmp = first;
  85. if(first==before){
  86. this.insertFirst(o);
  87. return;
  88. }
  89. //ako first!=before
  90. while (tmp.succ != before)
  91. tmp = tmp.succ;
  92. if (tmp.succ == before) {
  93. SLLNode<E> ins = new SLLNode<E>(o, before);
  94. tmp.succ = ins;
  95. } else {
  96. System.out.println("Elementot ne postoi vo listata");
  97. }
  98. } else {
  99. System.out.println("Listata e prazna");
  100. }
  101. }
  102.  
  103. public void insertLast(E o) {
  104. if (first != null) {
  105. SLLNode<E> tmp = first;
  106. while (tmp.succ != null)
  107. tmp = tmp.succ;
  108. SLLNode<E> ins = new SLLNode<E>(o, null);
  109. tmp.succ = ins;
  110. } else {
  111. insertFirst(o);
  112. }
  113. }
  114.  
  115. public E deleteFirst() {
  116. if (first != null) {
  117. SLLNode<E> tmp = first;
  118. first = first.succ;
  119. return tmp.element;
  120. } else {
  121. System.out.println("Listata e prazna");
  122. return null;
  123. }
  124. }
  125.  
  126. public E delete(SLLNode<E> node) {
  127. if (first != null) {
  128. SLLNode<E> tmp = first;
  129. if(first ==node){
  130. return this.deleteFirst();
  131. }
  132. while (tmp.succ != node&&tmp.succ.succ != null)
  133. tmp = tmp.succ;
  134. if (tmp.succ == node) {
  135. tmp.succ = tmp.succ.succ;
  136. return node.element;
  137. } else {
  138. System.out.println("Elementot ne postoi vo listata");
  139. return null;
  140. }
  141. } else {
  142. System.out.println("Listata e prazna");
  143. return null;
  144. }
  145.  
  146. }
  147.  
  148. public SLLNode<E> getFirst() {
  149. return first;
  150. }
  151.  
  152. public SLLNode<E> find(E o) {
  153. if (first != null) {
  154. SLLNode<E> tmp = first;
  155. while (tmp.element != o && tmp.succ != null)
  156. tmp = tmp.succ;
  157. if (tmp.element == o) {
  158. return tmp;
  159. } else {
  160. System.out.println("Elementot ne postoi vo listata");
  161. }
  162. } else {
  163. System.out.println("Listata e prazna");
  164. }
  165. return first;
  166. }
  167.  
  168. public Iterator<E> iterator () {
  169. // Return an iterator that visits all elements of this list, in left-to-right order.
  170. return new LRIterator<E>();
  171. }
  172.  
  173. // //////////Inner class ////////////
  174.  
  175. private class LRIterator<E> implements Iterator<E> {
  176.  
  177. private SLLNode<E> place, curr;
  178.  
  179. private LRIterator() {
  180. place = (SLLNode<E>) first;
  181. curr = null;
  182. }
  183.  
  184. public boolean hasNext() {
  185. return (place != null);
  186. }
  187.  
  188. public E next() {
  189. if (place == null)
  190. throw new NoSuchElementException();
  191. E nextElem = place.element;
  192. curr = place;
  193. place = place.succ;
  194. return nextElem;
  195. }
  196.  
  197. public void remove() {
  198. //Not implemented
  199. }
  200. }
  201.  
  202. public void mirror(){
  203. if (first != null) {
  204. //m=nextsucc, p=tmp,q=next
  205. SLLNode<E> tmp = first;
  206. SLLNode<E> newsucc = null;
  207. SLLNode<E> next;
  208.  
  209. while(tmp != null){
  210. next = tmp.succ;
  211. tmp.succ = newsucc;
  212. newsucc = tmp;
  213. tmp = next;
  214. }
  215. first = newsucc;
  216. }
  217.  
  218. }
  219.  
  220. public void merge (SLL<E> in){
  221. if (first != null) {
  222. SLLNode<E> tmp = first;
  223. while(tmp.succ != null)
  224. tmp = tmp.succ;
  225. tmp.succ = in.getFirst();
  226. }
  227. else{
  228. first = in.getFirst();
  229. }
  230. }
  231.  
  232.  
  233. }
  234.  
  235.  
  236. public class SpecialSLLJoin {
  237.  
  238. public static SLL<Integer> specialJoin(SLL<Integer> lista1,SLL<Integer> lista2){
  239. SLL<Integer> rezultat=new SLL<>();
  240. SLLNode<Integer>jazol1=lista1.getFirst();
  241. SLLNode<Integer>jazol2=lista2.getFirst();
  242.  
  243. while(jazol1!=null && jazol1.succ!=null && jazol2!=null && jazol2.succ!=null){
  244. rezultat.insertLast((Integer) jazol1.element);
  245. rezultat.insertLast((Integer) jazol1.succ.element);
  246.  
  247. rezultat.insertLast((Integer) jazol2.element);
  248. rezultat.insertLast((Integer) jazol2.succ.element);
  249.  
  250. jazol1=jazol1.succ.succ;
  251. jazol2=jazol2.succ.succ;
  252.  
  253. }
  254. while(jazol1!=null){
  255. rezultat.insertLast((Integer)jazol1.element);
  256. jazol1=jazol1.succ;
  257. }
  258.  
  259. while(jazol2!=null){
  260. rezultat.insertLast((Integer)jazol2.element);
  261. jazol2=jazol2.succ;
  262. }
  263. return rezultat;
  264. }
  265.  
  266. public static void main(String[] args) throws IOException{
  267.  
  268. BufferedReader stdin = new BufferedReader(new InputStreamReader(
  269. System.in));
  270. String s = stdin.readLine();
  271. int N = Integer.parseInt(s);
  272. s = stdin.readLine();
  273. String[] pomniza = s.split(" ");
  274. SLL<Integer> lista1=new SLL<>();
  275. for (int i = 0; i < N; i++) {
  276. lista1.insertLast(Integer.parseInt(pomniza[i]));
  277. }
  278.  
  279. s = stdin.readLine();
  280. N = Integer.parseInt(s);
  281. s = stdin.readLine();
  282. pomniza = s.split(" ");
  283. SLL<Integer> lista2=new SLL<>();
  284. for (int i = 0; i < N; i++) {
  285. lista2.insertLast(Integer.parseInt(pomniza[i]));
  286. }
  287. SLL<Integer> spoeni=new SLL<>();
  288. spoeni = specialJoin(lista1,lista2);
  289.  
  290. Iterator<Integer> it = spoeni.iterator();
  291. while (it.hasNext()) {
  292. System.out.print(it.next());
  293. if(it.hasNext())
  294. System.out.print(" ");
  295. }
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement