Martina312

Бришење подлисти

Feb 10th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class BrishiPodlisti {
  4.     public static boolean ifEqual(SLLNode<Integer> tmp1, SLL<Integer>list2){
  5.         SLLNode<Integer> tmp2=list2.getFirst();
  6.         for(int i=0;i<list2.length();i++){
  7.             if(tmp1.element!=tmp2.element){
  8.                 return false;
  9.             }else{
  10.                 tmp1=tmp1.succ;
  11.                 tmp2=tmp2.succ;
  12.                 if(tmp1==null && tmp2!=null)
  13.                     return false;
  14.             }
  15.         }
  16.         return true;
  17.     }
  18.     public static void izbrishi(SLL<Integer> list1, SLL<Integer> list2){
  19.         SLLNode<Integer> tmp1=list1.getFirst();
  20.         SLLNode<Integer> tmp2=list2.getFirst();
  21.  
  22.         while (tmp1!=null){
  23.             if(tmp1.element==tmp2.element){
  24.                 if(ifEqual(tmp1,list2)){
  25.                     for(int i=0;i<list2.length();i++){
  26.                         SLLNode<Integer> kopija=tmp1;
  27.                         list1.delete(tmp1);
  28.                         tmp1=kopija.succ;
  29.                     }
  30.                 }else{
  31.                     tmp1=tmp1.succ;
  32.                 }
  33.             }
  34.             else{
  35.                 tmp1=tmp1.succ;
  36.             }
  37.         }
  38.         System.out.println(list1);
  39.     }
  40.     public static void main(String[] args) {
  41.         Scanner in=new Scanner(System.in);
  42.  
  43.         int n1=in.nextInt();
  44.         SLL<Integer> list1=new SLL<>();
  45.         for(int i=0;i<n1;i++){
  46.             list1.insertLast(in.nextInt());
  47.         }
  48.  
  49.         int n2=in.nextInt();
  50.         SLL<Integer> list2=new SLL<>();
  51.         for(int i=0;i<n2;i++){
  52.             list2.insertLast(in.nextInt());
  53.         }
  54.  
  55.         izbrishi(list1,list2);
  56.     }
  57. }
Add Comment
Please, Sign In to add comment