Crazy

SLL prv karakter posleden vo zborot

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