Advertisement
Guest User

zajana

a guest
Sep 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class SLLNode<E> {
  5.  
  6. protected E element;
  7. protected SLLNode<E> succ;
  8.  
  9. public SLLNode(E elem, SLLNode<E> succ) {
  10. this.element = elem;
  11. this.succ = succ;
  12. }
  13.  
  14. @Override
  15. public String toString() {
  16. return element.toString();
  17. }
  18.  
  19. public void print() {
  20. SLLNode<E> ptr = this;
  21. while (ptr.succ != null) {
  22. System.out.println(ptr.element + " ");
  23. ptr = ptr.succ;
  24. }
  25. System.out.println(ptr.element);
  26. }
  27. }
  28.  
  29. class SLL<E> {
  30.  
  31. private SLLNode<E> first;
  32.  
  33. public SLL() {
  34. // Construct an empty SLL
  35. this.first = null;
  36. }
  37.  
  38. public void deleteList() {
  39. first = null;
  40. }
  41.  
  42. public int length() {
  43. int ret;
  44. if (first != null) {
  45. SLLNode<E> tmp = first;
  46. ret = 1;
  47. while (tmp.succ != null) {
  48. tmp = tmp.succ;
  49. ret++;
  50. }
  51. return ret;
  52. } else {
  53. return 0;
  54. }
  55.  
  56. }
  57.  
  58. @Override
  59. public String toString() {
  60. String ret = new String();
  61. if (first != null) {
  62. SLLNode<E> tmp = first;
  63. ret += tmp + "->";
  64. while (tmp.succ != null) {
  65. tmp = tmp.succ;
  66. ret += tmp + "->";
  67. }
  68. } else {
  69. ret = "Prazna lista!!!";
  70. }
  71. return ret;
  72. }
  73.  
  74. public void insertFirst(E o) {
  75. SLLNode<E> ins = new SLLNode<E>(o, first);
  76. first = ins;
  77. }
  78.  
  79. public void insertAfter(E o, SLLNode<E> node) {
  80. if (node != null) {
  81. SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  82. node.succ = ins;
  83. } else {
  84. System.out.println("Dadenot jazol e null");
  85. }
  86. }
  87.  
  88. public void insertBefore(E o, SLLNode<E> before) {
  89.  
  90. if (first != null) {
  91. SLLNode<E> tmp = first;
  92. if (first == before) {
  93. this.insertFirst(o);
  94. return;
  95. }
  96. //ako first!=before
  97. while (tmp.succ != before) {
  98. tmp = tmp.succ;
  99. }
  100. if (tmp.succ == before) {
  101. SLLNode<E> ins = new SLLNode<E>(o, before);
  102. tmp.succ = ins;
  103. } else {
  104. System.out.println("Elementot ne postoi vo listata");
  105. }
  106. } else {
  107. System.out.println("Listata e prazna");
  108. }
  109. }
  110.  
  111. public void insertLast(E o) {
  112. if (first != null) {
  113. SLLNode<E> tmp = first;
  114. while (tmp.succ != null) {
  115. tmp = tmp.succ;
  116. }
  117. SLLNode<E> ins = new SLLNode<E>(o, null);
  118. tmp.succ = ins;
  119. } else {
  120. insertFirst(o);
  121. }
  122. }
  123.  
  124. public E deleteFirst() {
  125. if (first != null) {
  126. SLLNode<E> tmp = first;
  127. first = first.succ;
  128. return tmp.element;
  129. } else {
  130. System.out.println("Listata e prazna");
  131. return null;
  132. }
  133. }
  134.  
  135. public E delete(SLLNode<E> node) {
  136. if (first != null) {
  137. SLLNode<E> tmp = first;
  138. if (first == node) {
  139. return this.deleteFirst();
  140. }
  141. while (tmp.succ != node && tmp.succ.succ != null) {
  142. tmp = tmp.succ;
  143. }
  144. if (tmp.succ == node) {
  145. tmp.succ = tmp.succ.succ;
  146. return node.element;
  147. } else {
  148. System.out.println("Elementot ne postoi vo listata");
  149. return null;
  150. }
  151. } else {
  152. System.out.println("Listata e prazna");
  153. return null;
  154. }
  155.  
  156. }
  157.  
  158. public SLLNode<E> getFirst() {
  159. return first;
  160. }
  161.  
  162. public SLLNode<E> find(E o) {
  163. if (first != null) {
  164. SLLNode<E> tmp = first;
  165. while (tmp.element != o && tmp.succ != null) {
  166. tmp = tmp.succ;
  167. }
  168. if (tmp.element == o) {
  169. return tmp;
  170. } else {
  171. System.out.println("Elementot ne postoi vo listata");
  172. }
  173. } else {
  174. System.out.println("Listata e prazna");
  175. }
  176. return first;
  177. }
  178.  
  179. public Iterator<E> iterator() {
  180. // Return an iterator that visits all elements of this list, in left-to-right order.
  181. return new LRIterator<E>();
  182. }
  183.  
  184. // //////////Inner class ////////////
  185. private class LRIterator<E> implements Iterator<E> {
  186.  
  187. private SLLNode<E> place, curr;
  188.  
  189. private LRIterator() {
  190. place = (SLLNode<E>) first;
  191. curr = null;
  192. }
  193.  
  194. public boolean hasNext() {
  195. return (place != null);
  196. }
  197.  
  198. public E next() {
  199. if (place == null) {
  200. throw new NoSuchElementException();
  201. }
  202. E nextElem = place.element;
  203. curr = place;
  204. place = place.succ;
  205. return nextElem;
  206. }
  207.  
  208. public void remove() {
  209. //Not implemented
  210. }
  211. }
  212.  
  213. public void mirror() {
  214. if (first != null) {
  215. //m=nextsucc, p=tmp,q=next
  216. SLLNode<E> tmp = first;
  217. SLLNode<E> newsucc = null;
  218. SLLNode<E> next;
  219.  
  220. while (tmp != null) {
  221. next = tmp.succ;
  222. tmp.succ = newsucc;
  223. newsucc = tmp;
  224. tmp = next;
  225. }
  226. first = newsucc;
  227. }
  228.  
  229. }
  230.  
  231. public void merge(SLL<E> in) {
  232. if (first != null) {
  233. SLLNode<E> tmp = first;
  234. while (tmp.succ != null) {
  235. tmp = tmp.succ;
  236. }
  237. tmp.succ = in.getFirst();
  238. } else {
  239. first = in.getFirst();
  240. }
  241. }
  242. }
  243.  
  244. public class fvgd {
  245. public static void smeni(SLL<Integer> lista,int x){
  246. SLLNode<Integer> dvizi=lista.getFirst();
  247. SLLNode<Integer> pomali=null;
  248. SLLNode<Integer> pogolemi=null;
  249.  
  250. int f1=1;
  251. int f2=1;
  252. while(dvizi!=null) {
  253.  
  254. if(dvizi.element<x && f1==1) {
  255. pomali=dvizi;
  256. f1=0;
  257. }
  258.  
  259. else if(dvizi.element>=x && f2==1) {
  260. pogolemi=dvizi;
  261. f2=0;
  262. }
  263.  
  264. if(f1==0 && f2==0) {
  265. break;
  266. }
  267. System.out.println("1");
  268. dvizi=dvizi.succ;
  269. }
  270.  
  271. dvizi=lista.getFirst();
  272. SLLNode<Integer> prvoPomal=pomali;
  273. SLLNode<Integer> prvoPogolem=pogolemi;
  274. while(dvizi.succ!=null) {
  275. System.out.println("element:" + dvizi.element);
  276. if(dvizi.element<x) {
  277. System.out.println("islov1");
  278. pomali.succ=dvizi;
  279. pomali=pomali.succ;
  280. System.out.println("pomali: "+pomali.element);
  281. System.out.println("dvizi: "+dvizi.element);
  282. System.out.println("divizi.succ"+dvizi.succ.element);
  283. System.out.println("divizi.succ.succ"+dvizi.succ.succ.element);
  284. }
  285. else {
  286. System.out.println("uslov2");
  287. pogolemi.succ=dvizi;
  288. pogolemi=pogolemi.succ;
  289. }
  290. System.out.println("2");
  291. dvizi=dvizi.succ;
  292. System.out.println("zavrsuvam so ciklus");
  293.  
  294. }
  295.  
  296. if(prvoPogolem==null) {
  297. pomali.succ=null;
  298. dvizi=prvoPomal;
  299. while(dvizi!=null) {
  300. if(dvizi.succ==null) {
  301. System.out.println(dvizi.element);
  302. break;
  303. }
  304. System.out.println(dvizi.element+"->");
  305. System.out.println("3");
  306. dvizi=dvizi.succ;
  307. }
  308.  
  309. }
  310. else if(prvoPomal==null) {
  311. pogolemi.succ=null;
  312. dvizi=prvoPogolem;
  313. while(dvizi!=null) {
  314. if(dvizi.succ==null) {
  315. System.out.println(dvizi.element);
  316. break;
  317. }
  318. System.out.println(dvizi.element+"->");
  319. System.out.println("4");
  320. dvizi=dvizi.succ;
  321. }
  322. }
  323. else {
  324. pomali.succ=prvoPogolem;
  325. pogolemi.succ=null;
  326. dvizi=prvoPomal;
  327. while(dvizi!=null) {
  328. if(dvizi.succ==null) {
  329. System.out.println(dvizi.element);
  330. break;
  331. }
  332. System.out.println(dvizi.element+"->");
  333. System.out.println("5");
  334. dvizi=dvizi.succ;
  335. }
  336.  
  337. }}
  338.  
  339.  
  340.  
  341. public static void main(String[] args) throws IOException {
  342. // TODO Auto-generated method stub
  343. SLL<Integer> lista = new SLL<Integer>();
  344. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  345. int N = Integer.parseInt(br.readLine());
  346. String line = br.readLine();
  347. String[] pom = line.split(" ");
  348. for (int i = 0; i < N; i++) {
  349. lista.insertLast(Integer.parseInt(pom[i]));
  350. }
  351. int x = Integer.parseInt(br.readLine());
  352. SLLNode<Integer> prv=lista.getFirst();
  353. if(lista.length()==1){
  354. System.out.print(prv.element);
  355.  
  356. }
  357. else{
  358.  
  359. smeni(lista,x);
  360. }
  361.  
  362. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement