Advertisement
SashkoKlincharov

[JAVA][АПС] - Подели листа (h)

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