Martina312

Промена

Feb 4th, 2020
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Promena {
  4.  
  5.     public static int zbir(SLL<Integer> list, int pos1, int pos2){
  6.         SLLNode<Integer> tmp=list.getFirst();
  7.         int brojac=0;
  8.         int sum=0;
  9.         while (brojac!=pos1){
  10.            tmp=tmp.succ;
  11.            brojac++;
  12.         }
  13.         while(brojac!=pos2+1){
  14.             sum+=tmp.element;
  15.             tmp=tmp.succ;
  16.             brojac++;
  17.         }
  18.         return sum;
  19.     }
  20.     public static void promeni(SLL<Integer> list, int pos1, int pos2){
  21.         int sum=zbir(list,pos1,pos2);
  22.         SLLNode<Integer> tmp=list.getFirst();
  23.  
  24.         while(tmp!=null){
  25.             tmp=tmp.succ;
  26.             if(tmp==null)
  27.                 break;
  28.             list.insertBefore(sum,tmp);
  29.             SLLNode<Integer> kopija=tmp;
  30.             list.delete(tmp);
  31.             tmp=kopija.succ;
  32.         }
  33.         System.out.println(list);
  34.     }
  35.     public static void main(String[] args) {
  36.         Scanner in=new Scanner(System.in);
  37.         int n=in.nextInt();
  38.  
  39.         SLL<Integer> list=new SLL<>();
  40.         for(int i=0;i<n;i++){
  41.             list.insertLast(in.nextInt());
  42.         }
  43.  
  44.         int pos1=in.nextInt();
  45.         int pos2=in.nextInt();
  46.  
  47.         promeni(list,pos1,pos2);
  48.  
  49.     }
  50. }
Add Comment
Please, Sign In to add comment