Crazy

Podeli spored samoglaska

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