Advertisement
Guest User

SpojListiNaizmenicno

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