Advertisement
Guest User

Untitled

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