Advertisement
Guest User

Za Janik :D

a guest
Nov 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1.  
  2. import java.util.Iterator;
  3. import java.util.NoSuchElementException;
  4. import java.util.Scanner;
  5.  
  6.  
  7. class SLLNode<E> {
  8. protected E element;
  9. protected SLLNode<E> succ;
  10.  
  11. public SLLNode(E elem, SLLNode<E> succ) {
  12. this.element = elem;
  13. this.succ = succ;
  14. }
  15.  
  16. @Override
  17. public String toString() {
  18. return element.toString();
  19. }
  20. }
  21.  
  22.  
  23. class SLL<E> {
  24. private SLLNode<E> first;
  25.  
  26. public SLL() {
  27. // Construct an empty SLL
  28. this.first = null;
  29. }
  30.  
  31. public void deleteList() {
  32. first = null;
  33. }
  34.  
  35. public int length() {
  36. int ret;
  37. if (first != null) {
  38. SLLNode<E> tmp = first;
  39. ret = 1;
  40. while (tmp.succ != null) {
  41. tmp = tmp.succ;
  42. ret++;
  43. }
  44. return ret;
  45. } else
  46. return 0;
  47.  
  48. }
  49.  
  50. @Override
  51. public String toString() {
  52. String ret = new String();
  53. if (first != null) {
  54. SLLNode<E> tmp = first;
  55. ret += tmp + "->";
  56. while (tmp.succ != null) {
  57. tmp = tmp.succ;
  58. ret += tmp + "->";
  59. }
  60. } else
  61. ret = "Prazna lista!!!";
  62. return ret;
  63. }
  64.  
  65. public void insertFirst(E o) {
  66. SLLNode<E> ins = new SLLNode<E>(o, first);
  67. first = ins;
  68. }
  69.  
  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. public SLLNode<E> getLast(){
  152. SLLNode<E> temp = first;
  153. while(temp.succ != null) {
  154. temp = temp.succ;
  155. }
  156. return temp;
  157. }
  158.  
  159. public SLLNode<E> find(E o) {
  160. if (first != null) {
  161. SLLNode<E> tmp = first;
  162. while (tmp.element != o && tmp.succ != null)
  163. tmp = tmp.succ;
  164. if (tmp.element == o) {
  165. return tmp;
  166. } else {
  167. System.out.println("Elementot ne postoi vo listata");
  168. }
  169. } else {
  170. System.out.println("Listata e prazna");
  171. }
  172. return first;
  173. }
  174.  
  175. public Iterator<E> iterator () {
  176. // Return an iterator that visits all elements of this list, in left-to-right order.
  177. return new LRIterator<E>();
  178. }
  179.  
  180. // //////////Inner class ////////////
  181.  
  182. private class LRIterator<E> implements Iterator<E> {
  183.  
  184. private SLLNode<E> place, curr;
  185.  
  186. private LRIterator() {
  187. place = (SLLNode<E>) first;
  188. curr = null;
  189. }
  190.  
  191. public boolean hasNext() {
  192. return (place != null);
  193. }
  194.  
  195. public E next() {
  196. if (place == null)
  197. throw new NoSuchElementException();
  198. E nextElem = place.element;
  199. curr = place;
  200. place = place.succ;
  201. return nextElem;
  202. }
  203.  
  204. public void remove() {
  205. //Not implemented
  206. }
  207. }
  208.  
  209. public int getSize() {
  210. int sum=0;
  211. SLLNode<E> temp = first;
  212. while(temp!=null) {
  213. sum++;
  214. temp=temp.succ;
  215. }
  216. return sum;
  217.  
  218. }
  219.  
  220. public void mirror(){
  221. if (first != null) {
  222. //m=nextsucc, p=tmp,q=next
  223. SLLNode<E> tmp = first;
  224. SLLNode<E> newsucc = null;
  225. SLLNode<E> next;
  226.  
  227. while(tmp != null){
  228. next = tmp.succ;
  229. tmp.succ = newsucc;
  230. newsucc = tmp;
  231. tmp = next;
  232. }
  233. first = newsucc;
  234. }
  235.  
  236. }
  237.  
  238.  
  239. public void merge (SLL<E> in){
  240. if (first != null) {
  241. SLLNode<E> tmp = first;
  242. while(tmp.succ != null)
  243. tmp = tmp.succ;
  244. tmp.succ = in.getFirst();
  245. }
  246. else{
  247. first = in.getFirst();
  248. }
  249. }
  250. }
  251.  
  252.  
  253. public class zadaca {
  254.  
  255.  
  256. public static void main (String [] arg) {
  257. Scanner sc = new Scanner(System.in);
  258. int n = sc.nextInt();
  259.  
  260. SLL<Integer> broevi = new SLL();
  261.  
  262. for(int i = 0 ; i < n ;i++) {
  263. broevi.insertLast(sc.nextInt());
  264. }
  265.  
  266. SLLNode<Integer> last=broevi.getLast();
  267. SLLNode<Integer> first = broevi.getFirst();
  268.  
  269.  
  270.  
  271. while(first != null) {
  272. broevi.insertAfter(last.element, first);
  273. //SLLNode<Integer> temp = last;
  274. broevi.delete(last);
  275. last = broevi.getLast();
  276. first = first.succ.succ;
  277. }
  278.  
  279.  
  280.  
  281.  
  282. //System.out.println(broevi.getSize());
  283.  
  284. System.out.println(broevi.toString());
  285.  
  286. }
  287.  
  288.  
  289.  
  290. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement