Martina312

Преврти од m до n

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