Advertisement
teodor_dalavera

Правилни реченици

Nov 8th, 2017
427
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.14 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.         SLL<Character> zbor = new SLL<Character>();
  236.         SLL<Character> rez = new SLL<Character>();
  237.         char bukva = 0;
  238.         SLLNode<Character> tmp = lista.getFirst();
  239.         SLLNode<Character> tmpg = lista.getFirst();
  240.         SLLNode<Character> tmpz = zbor.getFirst();
  241.        
  242.         while(tmpg != null){
  243.             while(tmp.element != ' ' && tmp.element != '.'){
  244.                 if(tmp.succ.element != ' ' && tmp.succ.element != '.'){
  245.                     zbor.insertLast(tmp.element);
  246.                     //System.out.println("zbor: " + tmp.element);
  247.                 }
  248.                 else{
  249.                     bukva = tmp.element;
  250.                     //System.out.println("bukva: " + bukva);
  251.                     zbor.insertFirst(bukva);
  252.                     if(tmp.succ.element == ' '){
  253.                         zbor.insertLast(' ');
  254.                     }
  255.                     else{
  256.                         zbor.insertLast('.');
  257.                     }
  258.                 }
  259.                 tmp = tmp.succ;
  260.             }
  261.             tmpz = zbor.getFirst();
  262.             while(tmpz != null){
  263.                 rez.insertLast(tmpz.element);
  264.                 //System.out.println(tmpz.element + " ");
  265.                 tmpz = tmpz.succ;
  266.             }
  267.             zbor.deleteList();
  268.             if(tmp.succ != null){
  269.                 tmp = tmp.succ;
  270.             }
  271.             tmpg = tmpg.succ;
  272.         }
  273.        
  274.        
  275.         return rez;
  276.     }
  277.    
  278.     public static void main(String[] args) throws Exception {
  279.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  280.        
  281.         String expression = br.readLine();
  282.        
  283.         String[] exp = expression.split("");
  284.        
  285.         SLL<Character> lista = new SLL<Character>();
  286.         SLL<Character> rez = new SLL<Character>();
  287.        
  288.         for(int i=0; i<exp.length; i++){
  289.             lista.insertLast(exp[i].charAt(0));
  290.         }
  291.        
  292.         rez = ispravi(lista);
  293.        
  294.         SLLNode<Character> tmp = rez.getFirst();
  295.        
  296.         while(tmp != null){
  297.             System.out.print(tmp.element);
  298.             tmp = tmp.succ;
  299.         }
  300.        
  301.         br.close();
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement