SlavkovB

[АПС] Двоцифрени броеви

Sep 16th, 2019
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.56 KB | None | 0 0
  1. /** Во првиот ред од влезот е даден бројот на јазли на влезната листа, потоа во вториот ред се дадени цифрите од кои се составени јазлите по редослед разделени со празно место.
  2. На излез треба да се испечатат јазлите на резултантната илста.
  3.  
  4. Пример влез:                                          Пример излез:
  5. 5                                                        4 56 67
  6. 4 5 6 6 7
  7.  
  8. Пример влез:
  9. 6                                                       Пример излез:
  10. 5 6 6 1 1 5                                              56 61 1 5
  11.  
  12. */
  13.  
  14.  
  15. //CODE
  16.  
  17. import java.util.Scanner;
  18.  
  19. class SLLNode<E> {
  20.     protected E element;
  21.     protected SLLNode<E> succ;
  22.  
  23.     public SLLNode(E elem, SLLNode<E> succ) {
  24.         this.element = elem;
  25.         this.succ = succ;
  26.     }
  27.  
  28.     @Override
  29.     public String toString() {
  30.         return element.toString();
  31.     }
  32. }
  33.  
  34. class SLL<E> {
  35.     private SLLNode<E> first;
  36.  
  37.     public SLL() {
  38.         // Construct an empty SLL
  39.         this.first = null;
  40.     }
  41.  
  42.     public void deleteList() {
  43.         first = null;
  44.     }
  45.  
  46.     public int length() {
  47.         int ret;
  48.         if (first != null) {
  49.             SLLNode<E> tmp = first;
  50.             ret = 1;
  51.             while (tmp.succ != null) {
  52.                 tmp = tmp.succ;
  53.                 ret++;
  54.             }
  55.             return ret;
  56.         } else
  57.             return 0;
  58.  
  59.     }
  60.  
  61.     @Override
  62.     public String toString() {
  63.         String ret = new String();
  64.         if (first != null) {
  65.             SLLNode<E> tmp = first;
  66.             ret += tmp + " ";
  67.             while (tmp.succ != null) {
  68.                 tmp = tmp.succ;
  69.                 ret += tmp + " ";
  70.             }
  71.         } else
  72.             ret = "Prazna lista!!!";
  73.         return ret;
  74.     }
  75.  
  76.     public void setFirst(SLLNode<E> first) {
  77.         this.first = first;
  78.     }
  79.  
  80.     public void insertFirst(E o) {
  81.         SLLNode<E> ins = new SLLNode<E>(o, first);
  82.         first = ins;
  83.     }
  84.  
  85.     public void insertAfter(E o, SLLNode<E> node) {
  86.         if (node != null) {
  87.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  88.             node.succ = ins;
  89.         } else {
  90.             System.out.println("Dadenot jazol e null");
  91.         }
  92.     }
  93.  
  94.     public void insertBefore(E o, SLLNode<E> before) {
  95.  
  96.         if (first != null) {
  97.             SLLNode<E> tmp = first;
  98.             if (first == before) {
  99.                 this.insertFirst(o);
  100.                 return;
  101.             }
  102.             // ako first!=before
  103.             while (tmp.succ != before)
  104.                 tmp = tmp.succ;
  105.             if (tmp.succ == before) {
  106.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  107.                 tmp.succ = ins;
  108.             } else {
  109.                 System.out.println("Elementot ne postoi vo listata");
  110.             }
  111.         } else {
  112.             System.out.println("Listata e prazna");
  113.         }
  114.     }
  115.  
  116.     public void insertLast(E o) {
  117.         if (first != null) {
  118.             SLLNode<E> tmp = first;
  119.             while (tmp.succ != null)
  120.                 tmp = tmp.succ;
  121.             SLLNode<E> ins = new SLLNode<E>(o, null);
  122.             tmp.succ = ins;
  123.         } else {
  124.             insertFirst(o);
  125.         }
  126.     }
  127.  
  128.     public E deleteFirst() {
  129.         if (first != null) {
  130.             SLLNode<E> tmp = first;
  131.             first = first.succ;
  132.             return tmp.element;
  133.         } else {
  134.             System.out.println("Listata e prazna");
  135.             return null;
  136.         }
  137.     }
  138.  
  139.     public E delete(SLLNode<E> node) {
  140.         if (first != null) {
  141.             SLLNode<E> tmp = first;
  142.             if (first == node) {
  143.                 return this.deleteFirst();
  144.             }
  145.             while (tmp.succ != node && tmp.succ.succ != null)
  146.                 tmp = tmp.succ;
  147.             if (tmp.succ == node) {
  148.                 tmp.succ = tmp.succ.succ;
  149.                 return node.element;
  150.             } else {
  151.                 System.out.println("Elementot ne postoi vo listata");
  152.                 return null;
  153.             }
  154.         } else {
  155.             System.out.println("Listata e prazna");
  156.             return null;
  157.         }
  158.  
  159.     }
  160.  
  161.     public SLLNode<E> getFirst() {
  162.         return first;
  163.     }
  164.  
  165.     public SLLNode<E> find(E o) {
  166.         if (first != null) {
  167.             SLLNode<E> tmp = first;
  168.             while (tmp.element != o && tmp.succ != null)
  169.                 tmp = tmp.succ;
  170.             if (tmp.element == o) {
  171.                 return tmp;
  172.             } else {
  173.                 System.out.println("Elementot ne postoi vo listata");
  174.             }
  175.         } else {
  176.             System.out.println("Listata e prazna");
  177.         }
  178.         return first;
  179.     }
  180.  
  181. }
  182.  
  183. public class DvocifreniBroevi {
  184.  
  185.     public static void doubleDigitNumbers(SLL<Integer> list) {
  186.  
  187.         SLLNode<Integer> current = list.getFirst();
  188.  
  189.         while (current != null) {
  190.             if (current.element == 5 || current.element > 5) {
  191.                 if (current.succ == null)
  192.                     break;
  193.                 list.insertBefore((current.element * 10 + current.succ.element), current);
  194.                 list.delete(current);
  195.                 current = current.succ;
  196.                 list.delete(current);
  197.             }
  198.             current = current.succ;
  199.         }
  200.  
  201.         System.out.println(list);
  202.     }
  203.  
  204.     public static void main(String[] args) {
  205.         Scanner input = new Scanner(System.in);
  206.  
  207.         int n = input.nextInt();
  208.         SLL<Integer> list = new SLL<Integer>();
  209.  
  210.         for (int i = 0; i < n; ++i)
  211.             list.insertLast(input.nextInt());
  212.  
  213.         doubleDigitNumbers(list);
  214.         input.close();
  215.     }
  216. }
Add Comment
Please, Sign In to add comment