Advertisement
mitkomitrov

Untitled

Sep 5th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. package mirror;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.IOException;
  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. class SLL<E> {
  23. SLLNode<E> first;
  24.  
  25. public SLL() {
  26. // Construct an empty SLL
  27. this.first = null;
  28. }
  29.  
  30. public void deleteList() {
  31. first = null;
  32. }
  33.  
  34. public int length() {
  35. int ret;
  36. if (first != null) {
  37. SLLNode<E> tmp = first;
  38. ret = 1;
  39. while (tmp.succ != null) {
  40. tmp = tmp.succ;
  41. ret++;
  42. }
  43. return ret;
  44. } else
  45. return 0;
  46.  
  47. }
  48.  
  49. @Override
  50. public String toString()
  51. {
  52. SLLNode<E> current = first;
  53. String s = new String();
  54. while(current!=null)
  55. {
  56. s = s + current.element + "->";
  57. current = current.succ;
  58. }
  59. return s;
  60. }
  61.  
  62. public void insertFirst(E o) {
  63. SLLNode<E> ins = new SLLNode<E>(o, first);
  64. first = ins;
  65. }
  66.  
  67. public void insertAfter(E o, SLLNode<E> node) {
  68. if (node != null) {
  69. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  70. node.succ = ins;
  71. } else {
  72. System.out.println("Dadenot jazol e null");
  73. }
  74. }
  75.  
  76. public void insertBefore(E o, SLLNode<E> before) {
  77.  
  78. if (first != null) {
  79. SLLNode<E> tmp = first;
  80. if(first==before){
  81. this.insertFirst(o);
  82. return;
  83. }
  84. //ako first!=before
  85. while (tmp.succ != before)
  86. tmp = tmp.succ;
  87. if (tmp.succ == before) {
  88. SLLNode<E> ins = new SLLNode<E>(o, before);
  89. tmp.succ = ins;
  90. } else {
  91. System.out.println("Elementot ne postoi vo listata");
  92. }
  93. } else {
  94. System.out.println("Listata e prazna");
  95. }
  96. }
  97.  
  98. public void insertLast(E o) {
  99. if (first != null) {
  100. SLLNode<E> tmp = first;
  101. while (tmp.succ != null)
  102. tmp = tmp.succ;
  103. SLLNode<E> ins = new SLLNode<E>(o, null);
  104. tmp.succ = ins;
  105. } else {
  106. insertFirst(o);
  107. }
  108. }
  109.  
  110. public E deleteFirst() {
  111. if (first != null) {
  112. SLLNode<E> tmp = first;
  113. first = first.succ;
  114. return tmp.element;
  115. } else {
  116. System.out.println("Listata e prazna");
  117. return null;
  118. }
  119. }
  120.  
  121. public E delete(SLLNode<E> node) {
  122. if (first != null) {
  123. SLLNode<E> tmp = first;
  124. if(first ==node){
  125. return this.deleteFirst();
  126. }
  127. while (tmp.succ != node && tmp.succ.succ != null)
  128. tmp = tmp.succ;
  129. if (tmp.succ == node) {
  130. tmp.succ = tmp.succ.succ;
  131. return node.element;
  132. } else {
  133. System.out.println("Elementot ne postoi vo listata");
  134. return null;
  135. }
  136. } else {
  137. System.out.println("Listata e prazna");
  138. return null;
  139. }
  140.  
  141. }
  142.  
  143. public SLLNode<E> getFirst() {
  144. return first;
  145. }
  146.  
  147. public SLLNode<E> find(E o) {
  148. if (first != null) {
  149. SLLNode<E> tmp = first;
  150. while (tmp.element != o && tmp.succ != null)
  151. tmp = tmp.succ;
  152. if (tmp.element == o) {
  153. return tmp;
  154. } else {
  155. System.out.println("Elementot ne postoi vo listata");
  156. }
  157. } else {
  158. System.out.println("Listata e prazna");
  159. }
  160. return first;
  161. }
  162.  
  163.  
  164.  
  165. }
  166. public class Mirror {
  167. public static void mirror(SLL<Integer> lista)
  168. {
  169. SLLNode<Integer> current = lista.first;
  170. SLLNode<Integer> sled = null;
  171. SLLNode<Integer> pred = null;
  172. while(current != null)
  173. {
  174. sled = current.succ;
  175. current.succ = pred;
  176. pred = current;
  177. current = sled;
  178. }
  179. lista.first = pred;
  180.  
  181. }
  182.  
  183. /* public static String prevrti_Opseg(SLL<Integer> lista,int n, int m)
  184. {
  185. SLLNode<Integer> current = lista.first;
  186. SLLNode<Integer> N = null;
  187. SLLNode<Integer> M = null;
  188. int i = 1;
  189.  
  190. while(current != null)
  191. {
  192. if( i == n)
  193. {
  194. N = current;
  195. }
  196. else if ( i == m)
  197. {
  198. M = current;
  199. }
  200. i++;
  201. current = current.succ;
  202. }
  203. return "N e: " + N.element + " \n M e: " + M.element;
  204. }
  205. */
  206. public static void moment(SLL<Integer> lista, int x)
  207. {
  208. SLLNode<Integer> current = lista.first;
  209. SLLNode<Integer> p = current;
  210. SLLNode<Integer> rezerva = null;
  211. int flag = 0, i = 1;
  212. while(current.succ != null)
  213. {
  214. if(current.succ.element == x && flag == 0)
  215. {
  216. rezerva = current.succ;
  217. SLLNode<Integer> next = current.succ;
  218. current.succ = next.succ;
  219. flag = 1;
  220. i++;
  221. }
  222. else
  223. {
  224. current = current.succ;
  225. if(flag == 0)
  226. {
  227. i++;
  228. }
  229. }
  230. }
  231. if(flag == 0)
  232. {
  233. System.out.println("-1");
  234. }
  235. // INSERT FIRST THE ELEMENT EQUALS TO X
  236. /*
  237. SLLNode<Integer> ins = new SLLNode<Integer>(rezerva.element, lista.first);
  238. lista.first = ins;
  239. System.out.println("pozicija: "+ i);
  240. */
  241. // ===> DA GO STAVI NA KRAJ
  242.  
  243. //INSERT LAST THE LEMENT EQUALS TO X
  244.  
  245.  
  246. SLLNode<Integer> move = lista.first;
  247.  
  248. while(move.succ != null)
  249. {
  250. move = move.succ;
  251. }
  252. SLLNode<Integer> ins = new SLLNode<Integer>(rezerva.element, null);
  253. move.succ = ins;
  254.  
  255. System.out.println("move e: " + move.element);
  256. // move.succ = rezerva;
  257. System.out.println("move.succ e: " + move.succ.element);
  258. // move = rezerva;
  259. }
  260. public static void removeFirst(SLL<Integer> lista)
  261. {
  262. SLLNode<Integer> current = lista.first;
  263.  
  264. current = current.succ;
  265. lista.first = current;
  266. }
  267.  
  268.  
  269. public static void main(String[] args) throws IOException {
  270.  
  271. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  272.  
  273. SLL<Integer> lista = new SLL();
  274. int broevi, N, M;
  275. broevi = Integer.parseInt(input.readLine());
  276. N = Integer.parseInt(input.readLine());
  277. //M = Integer.parseInt(input.readLine());
  278. System.out.println("Vnesi: ");
  279. String line = input.readLine();
  280. String[] pom = line.split(" ");
  281. for(int i = 0; i < broevi; i++)
  282. {
  283. lista.insertLast(Integer.parseInt(pom[i]));
  284. }
  285.  
  286. // prevrti_Opseg(lista, 3, 5);
  287.  
  288. // ====> FUNKCIJAVA TREBA DA GO NAJDE ELEMENTOT I DA GO STAVI NA POCETOK/KRAJOT OD LISTATA
  289. moment(lista, N);
  290. System.out.println(lista);
  291.  
  292. }
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement