Advertisement
SlavkovB

[АПС] Сума на предходни елементи

Sep 15th, 2019
994
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.88 KB | None | 0 0
  1. /**Дадена е еднострано поврзана листа и да се модифицира листата на тој начин што ќе се почне од првиот јазел, вредноста на тој јазел се зема како вредност за локација после колку јазли треба да се вметне јазел со сумата на јазлите до таа локација. Откако ќе се вметне јазел се прдодолжува од наредниот и се повторува се додека не се стигне на крај од листата.
  2.  
  3. Влез:
  4. 14
  5. 2 3 4 5 2 4 5 7 5 9 2 3 4 13
  6. Излез:
  7. 2 3 5 4 5 2 4 15 5 7 5 9 2 28 3 4 13
  8.  
  9.  
  10. Влез:
  11. 15
  12. 3 3 4 5 2 4 5 7 5 9 2 3 4 13 2
  13. Излез:
  14. 3 3 4 10 5 2 4 5 7 23 5 9 2 3 4 23 13 2
  15. */
  16.  
  17. //CODE
  18.  
  19.  
  20. import java.util.Scanner;
  21.  
  22. class SLLNode<E> {
  23.     protected E element;
  24.     protected SLLNode<E> succ;
  25.  
  26.     public SLLNode(E elem, SLLNode<E> succ) {
  27.         this.element = elem;
  28.         this.succ = succ;
  29.     }
  30.  
  31.     @Override
  32.     public String toString() {
  33.         return element.toString();
  34.     }
  35. }
  36.  
  37. class SLL<E> {
  38.     private SLLNode<E> first;
  39.  
  40.     public SLL() {
  41.         // Construct an empty SLL
  42.         this.first = null;
  43.     }
  44.  
  45.     public void deleteList() {
  46.         first = null;
  47.     }
  48.  
  49.     public int length() {
  50.         int ret;
  51.         if (first != null) {
  52.             SLLNode<E> tmp = first;
  53.             ret = 1;
  54.             while (tmp.succ != null) {
  55.                 tmp = tmp.succ;
  56.                 ret++;
  57.             }
  58.             return ret;
  59.         } else
  60.             return 0;
  61.  
  62.     }
  63.  
  64.     @Override
  65.     public String toString() {
  66.         String ret = new String();
  67.         if (first != null) {
  68.             SLLNode<E> tmp = first;
  69.             ret += tmp + "->";
  70.             while (tmp.succ != null) {
  71.                 tmp = tmp.succ;
  72.                 ret += tmp + "->";
  73.             }
  74.         } else
  75.             ret = "Prazna lista!!!";
  76.         return ret;
  77.     }
  78.  
  79.     public void insertFirst(E o) {
  80.         SLLNode<E> ins = new SLLNode<E>(o, first);
  81.         first = ins;
  82.     }
  83.  
  84.     public void insertAfter(E o, SLLNode<E> node) {
  85.         if (node != null) {
  86.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  87.             node.succ = ins;
  88.         } else {
  89.             System.out.println("Dadenot jazol e null");
  90.         }
  91.     }
  92.  
  93.     public void insertBefore(E o, SLLNode<E> before) {
  94.  
  95.         if (first != null) {
  96.             SLLNode<E> tmp = first;
  97.             if (first == before) {
  98.                 this.insertFirst(o);
  99.                 return;
  100.             }
  101.             // ako first!=before
  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. public class SumBeforeGivenPosition {
  182.     private static void sumBefore(SLL<Integer> list) {
  183.         SLLNode<Integer> current = list.getFirst();
  184.         int sum = 0, count = 0, goal = 0;
  185.         boolean firstTime = true;
  186.  
  187.         while (current != null) {
  188.             if (firstTime) {
  189.                 goal = current.element;
  190.                 firstTime = false;
  191.             }
  192.  
  193.             if (count < goal && current != null) {
  194.                 sum += current.element;
  195.                 ++count;
  196.             } else {
  197.                 list.insertBefore(sum, current);
  198.                 sum = current.element;
  199.                 count = 1;
  200.                 goal = current.element;
  201.             }
  202.             current = current.succ;
  203.         }
  204.  
  205.         current = list.getFirst();
  206.  
  207.         while (current != null) {
  208.             System.out.print(current + " ");
  209.             current = current.succ;
  210.         }
  211.     }
  212.  
  213.     public static void main(String[] args) {
  214.         SLL<Integer> list = new SLL<Integer>();
  215.  
  216.         Scanner input = new Scanner(System.in);
  217.  
  218.         int n = input.nextInt();
  219.  
  220.         for (int i = 0; i < n; i++)
  221.             list.insertLast(input.nextInt());
  222.  
  223.         sumBefore(list);
  224.         input.close();
  225.     }
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement