Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 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.  
  7. class SLLNode<E> {
  8. protected E element;
  9. protected SLLNode<E> succ;
  10.  
  11. public SLLNode(E elem, SLLNode<E> succ) {
  12. this.element = elem;
  13. this.succ = succ;
  14. }
  15.  
  16. @Override
  17. public String toString() {
  18. return element.toString();
  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.  
  232. public class Main {
  233.  
  234. private static SLL<Integer> izbrishiPosleden(SLL<Integer> lista, int broj) {
  235. SLLNode<Integer> tmp= lista.getFirst();
  236. SLLNode<Integer> posl = null;
  237.  
  238. while (tmp!=null) {
  239. if (tmp.element.equals(broj)) {
  240. posl = tmp;
  241. tmp = tmp.succ;
  242. }
  243. else {
  244. tmp = tmp.succ;
  245. }
  246. }
  247.  
  248. lista.delete(posl);
  249.  
  250.  
  251.  
  252.  
  253. return lista;
  254.  
  255. }
  256.  
  257. public static void main(String[] args) throws IOException{
  258. // write your code here
  259. SLL<Integer> lista = new SLL<>();
  260.  
  261. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  262. String s = br.readLine();
  263. int N = Integer.parseInt(s);
  264.  
  265. for (int i = 0 ; i < N; i++) {
  266. s = br.readLine();
  267. lista.insertLast(Integer.parseInt(s));
  268. }
  269.  
  270. s = br.readLine();
  271. int broj = Integer.parseInt(s);
  272.  
  273. System.out.println(izbrishiPosleden(lista,broj));
  274. }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement