Advertisement
Guest User

Untitled

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