Advertisement
teodor_dalavera

Правилни реченици - Една листа

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