SlavkovB

[АПС] Сума на јазли

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