הפונקציות של היום 07.09.2025

public static void printList(Node<Integer> lst) {

                while (lst != null) {

                                System.out.print(lst.getValue()+ " –> ");

                                lst = lst.getNext();

                }

                System.out.println("null");

}

               

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

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

                if (first == null) {

                                first = toAdd;

                                return first;

                }

                if (first.getValue()>num) {

                                toAdd.setNext(first);

                                first = toAdd;

                                return first;

                }

                Node<Integer>prev = first;

                Node<Integer>curr = prev.getNext();

                while (curr != null && curr.getValue() < num) {

                                prev = prev.getNext();

                                curr = prev.getNext();

                }

                prev.setNext(toAdd);

                toAdd.setNext(curr);

                return first;

}

               

               

               

               

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

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

                if (first == null)

                                return toAdd;

                Node<Integer> p = first;

                while (p.getNext() != null) {

                                p = p.getNext();

                }

                p.setNext(toAdd);

                return first;

               

}

               

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

               

                Node<Integer> p = new Node<Integer>(num,first);                    

                return p;

               

}

public static void main(String[] args) {

               

                Node<Integer> lst = null;

                int num;

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

                num = (int)(Math.random()*100+3);

                                               

                                lst = insertIntoSortedList(lst, num);

                                printList(lst);

                }                                                                             

}