הפעולות שעשינו היום 07.10.2018

 

 

public static int maxInIntList(IntNode p){

       int max = p.getValue();

       while (p != null){

              if (p.getValue() > max)

                     max = p.getValue();

              p = p.getNext();

       }

       return max;

}

      

public static IntNode maxNodeInList(IntNode p){

       int max = p.getValue();

       IntNode q = p;

       while (p != null){

              if (p.getValue() > max){

                     max = p.getValue();

                     q = p;

              }

              p = p.getNext();

       }

       return q;

}

      

      

public static IntNode removeNode(IntNode first,IntNode p){

       if (p == first){

              return first.getNext();

       }

       IntNode prev = first;

       while (prev.getNext() != p)

              prev = prev.getNext();

       prev.setNext(p.getNext());

       return first;

}

 

public static void removeNodesFromList(IntNode first,int n){

       IntNode p;

       for (int i=0;i<n;i++){

              p = maxNodeInList(first);

              first = removeNode(first,p);

       }

}

 

public static Node<Integer> InsertInLast(Node<Integer> first,int num){

       Node<Integer> tmp = new Node<Integer>(num);

       if (first == null)

              return tmp;

       Node<Integer> p = first;

       while (p.hasNext())

              p = p.getNext();

      

       p.setNext(tmp);

       return first;

}

      

public static Node<Integer> merge(Node<Integer> lst1,Node<Integer> lst2){

       Node<Integer> lst3 = null;

       while (lst1 != null && lst2 != null){

              if (lst1.getValue()<lst2.getValue()){

                     lst3 = InsertInLast(lst3, lst1.getValue());

                     lst1 = lst1.getNext();

              }

              else{

                     lst3 = InsertInLast(lst3, lst2.getValue());

                     lst2 = lst2.getNext();

              }

       }

       while (lst1 != null){

              lst3 = InsertInLast(lst3, lst1.getValue());

              lst1 = lst1.getNext();

       }

       while (lst2 != null){

              lst3 = InsertInLast(lst3, lst2.getValue());

              lst2 = lst2.getNext();

       }

       return lst3;

}

      

public static boolean IsInList(Node<Integer> p,int num){

       while (p != null){

              if (p.getValue() == num)

                     return true;

              p = p.getNext();

       }

       return false;

}

      

public static Node<Integer> cutList(Node<Integer> lst1, Node<Integer>lst2){

       Node<Integer> lst3 = null;

       Node<Integer> p1 = lst1,p2 = lst2;

       while (p2 != null){

              if (IsInList(p1, p2.getValue()) && !IsInList(lst3, p2.getValue()) ){

                     InsertInLast(lst3, p2.getValue());

              }

              p2 = p2.getNext();

       }

       return lst3;

}