Advertisement
SlavkovB

[АПС] Броеви од цифри

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