Advertisement
mitkomitrov

Untitled

Sep 6th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.77 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package examples;
  7. import java.io.IOException;
  8. import java.io.BufferedReader;
  9. import java.io.InputStreamReader;
  10.  
  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. 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. {
  56. SLLNode<E> current = first;
  57. String s = new String();
  58. while(current!=null)
  59. {
  60. s = s + current.element + "->";
  61. current = current.succ;
  62. }
  63. return s;
  64. }
  65.  
  66. public void insertFirst(E o) {
  67. SLLNode<E> ins = new SLLNode<E>(o, first);
  68. first = ins;
  69. }
  70.  
  71. public void insertAfter(E o, SLLNode<E> node) {
  72. if (node != null) {
  73. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  74. node.succ = ins;
  75. } else {
  76. System.out.println("Dadenot jazol e null");
  77. }
  78. }
  79.  
  80. public void insertBefore(E o, SLLNode<E> before) {
  81.  
  82. if (first != null) {
  83. SLLNode<E> tmp = first;
  84. if(first==before){
  85. this.insertFirst(o);
  86. return;
  87. }
  88. //ako first!=before
  89. while (tmp.succ != before)
  90. tmp = tmp.succ;
  91. if (tmp.succ == before) {
  92. SLLNode<E> ins = new SLLNode<E>(o, before);
  93. tmp.succ = ins;
  94. } else {
  95. System.out.println("Elementot ne postoi vo listata");
  96. }
  97. } else {
  98. System.out.println("Listata e prazna");
  99. }
  100. }
  101.  
  102. public void insertLast(E o) {
  103. if (first != null) {
  104. SLLNode<E> tmp = first;
  105. while (tmp.succ != null)
  106. tmp = tmp.succ;
  107. SLLNode<E> ins = new SLLNode<E>(o, null);
  108. tmp.succ = ins;
  109. } else {
  110. insertFirst(o);
  111. }
  112. }
  113.  
  114. public E deleteFirst() {
  115. if (first != null) {
  116. SLLNode<E> tmp = first;
  117. first = first.succ;
  118. return tmp.element;
  119. } else {
  120. System.out.println("Listata e prazna");
  121. return null;
  122. }
  123. }
  124.  
  125. public E delete(SLLNode<E> node) {
  126. if (first != null) {
  127. SLLNode<E> tmp = first;
  128. if(first ==node){
  129. return this.deleteFirst();
  130. }
  131. while (tmp.succ != node && tmp.succ.succ != null)
  132. tmp = tmp.succ;
  133. if (tmp.succ == node) {
  134. tmp.succ = tmp.succ.succ;
  135. return node.element;
  136. } else {
  137. System.out.println("Elementot ne postoi vo listata");
  138. return null;
  139. }
  140. } else {
  141. System.out.println("Listata e prazna");
  142. return null;
  143. }
  144.  
  145. }
  146.  
  147. public SLLNode<E> getFirst() {
  148. return first;
  149. }
  150.  
  151. public SLLNode<E> find(E o) {
  152. if (first != null) {
  153. SLLNode<E> tmp = first;
  154. while (tmp.element != o && tmp.succ != null)
  155. tmp = tmp.succ;
  156. if (tmp.element == o) {
  157. return tmp;
  158. } else {
  159. System.out.println("Elementot ne postoi vo listata");
  160. }
  161. } else {
  162. System.out.println("Listata e prazna");
  163. }
  164. return first;
  165. }
  166.  
  167.  
  168.  
  169. }
  170. public class Examples {
  171.  
  172. public static void prevrti(SLL<Integer> lista)
  173. {
  174. SLLNode<Integer> current = lista.first;
  175. SLLNode<Integer> sled = null;
  176. SLLNode<Integer> pred = null;
  177. while(current != null)
  178. {
  179. sled = current.succ;
  180. current.succ = pred;
  181. pred = current;
  182. current = sled;
  183. }
  184. lista.first = pred;
  185. }
  186. /* ====> ZADACATA <===== */
  187. public static int example4(SLL<Integer> lista, int x)
  188. {
  189. SLLNode<Integer> current = lista.first;
  190. if (current.element == x)
  191. {
  192. return 1;
  193. }
  194. SLLNode<Integer> oldCurrent = lista.first;
  195. SLLNode<Integer> rezerva = null;
  196.  
  197. int pos = 1, flag = 0;
  198.  
  199. while (current != null)
  200. {
  201. SLLNode<Integer> sled = current.succ;
  202. if(sled.succ != null) // spravuvanje so NullPointerExcepton
  203. {
  204. if (sled.element == x)
  205. {
  206. rezerva = sled;
  207. current.succ = sled.succ;
  208. lista.first = rezerva;
  209. rezerva.succ = oldCurrent;
  210. pos++;
  211. return pos;
  212. }
  213. else
  214. {
  215. flag = 0;
  216. current = current.succ;
  217. pos++;
  218. }
  219.  
  220. }
  221. else
  222. {
  223. break;
  224. }
  225.  
  226. }
  227. if(flag == 0)
  228. {
  229. return -1;
  230. }
  231. else
  232. {
  233. return pos;
  234. }
  235. }
  236.  
  237. public static void main(String[] args) throws IOException {
  238. BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
  239.  
  240. SLL<Integer> lista = new SLL();
  241. int broevi, N, M;
  242. broevi = Integer.parseInt(input.readLine());
  243. N = Integer.parseInt(input.readLine());
  244. //M = Integer.parseInt(input.readLine());
  245. System.out.println("Vnesi: ");
  246. String line = input.readLine();
  247. String[] pom = line.split(" ");
  248. for(int i = 0; i < broevi; i++)
  249. {
  250. lista.insertLast(Integer.parseInt(pom[i]));
  251. }
  252.  
  253. int pos = example4(lista, N);
  254. System.out.println(lista);
  255. System.out.println(pos);
  256. }
  257.  
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement