Martina312

Преуреди листа

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