Advertisement
Guest User

Untitled

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