ivanakarpuzova

Untitled

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