'Re-order single linked list if elements have been searched?

public class SearchLinkedList<E> {
    private Node<E> first;

    public static void main(String[] args){
     SearchLinkedList<Integer> list = new SearchLinkedList<Integer>();
     
     list.insert(1000);
     list.insert(2000);
     list.insert(3000);
     
     System.out.println(list.getFirst());
   }

    public SearchLinkedList() {
      first = null;
   }

    public void insert(E e) {
        if (first == null) {
            first = new Node<E>(e);
        } else {
            //while(temp.next.searched == true) then insert new Node where the next node is null or searched == false
            Node<E> temp = new Node<E>(e);
            temp.next = first;
            first = temp;
        }
    }
    
    public E getFirst() {
        return first.data;
    }

    public E find(E x) {
        if (first == null) {
            return null;
        } else {
            //while (temp != null) if node found set it's searched = true and move it to front of list
            Node<E> temp = first;
            while (temp != null) {
                if (temp.data.equals(x)) {
                    temp.searched = true;
                    return temp.data;
                }
                temp = temp.next;
            }
            return temp.data;
        }
    }

    private static class Node<E> {
        private E data;
        private boolean searched;
        private Node<E> next;

        private Node(E e) {
            data = e;
            searched = false;
            next = null;
        }
    }
}

So the assignment here is to create a LinkedList class that moves a node to the front of the list (first) if it has been searched. First image here is when this is called:

list.insert(1000);
list.insert(2000);
list.insert(3000);

Second image is when this is called:

list.find(3000);
list.find(2000);

enter image description here

enter image description here

So the goal is when find is called and node with data is found: Set it's searched boolean to true and move that node to front of list. As of now, my insert just puts new node at the front of list. The comments in insert and find explain what I want to make them do. However, moving an element from the middle of a single linkedlist to front seems hard. Don't know what to do here. You can copy and try this yourself. After calling list.find(2000); and then list.getFirst() We should get 2000. Question is how... My thoughts are on if I should let the Node's booleans decide whether to be infront or not... Not sure here at all.


Thanks to danilllo19 for the help. That answer is correct only issue was that the new elements should be added from the front and so if an element is searched we're supposed to add the new one after the last searched but at the head of the un-serached ones. Solved it like this:

public void insert(E e) {
    if(first == null){
        first = new Node<E>(e);
    }else{
        Node<E> temp = first;
        while(temp.searched && temp.next != null && temp.next.searched){
            temp = temp.next;
        }
        Node<E> node = new Node<E>(e);
        
        if(temp.searched && temp.next != null && !temp.next.searched){
            Node<E> temp2 = temp.next;
            
            temp.next = node;
            node.next = temp2;
        }else if(temp.searched && temp.next == null){
            temp.next = node;
        }else{
            node.next = temp;
            first = node; 
        }
        
    }
}


Solution 1:[1]

I suppose you should do it like this:

public class SearchLinkedList<E> {
private Node<E> first;

public static void main(String[] args) {
    SearchLinkedList<Integer> list = new SearchLinkedList<Integer>();

    list.insert(1000);
    list.insert(2000);
    list.insert(3000);

    System.out.println(list.getFirst());

    System.out.println(list.find(3000));
    System.out.println(list.getFirst());
    list.insert(4000);
    System.out.println(list.find(200));
}

public SearchLinkedList() {
    first = null;
}

public void insert(E e) {
    if (first == null) {
        first = new Node<E>(e);
    } else {
        //while(temp.next.searched == true) then insert new Node where the next node is null or searched == false
        Node<E> temp = first;
        while (temp.next != null && temp.next.searched) {
            temp = temp.next;
        }
        Node<E> node = new Node<>(e);
        if (temp.next != null) {
            node.next = temp.next;
        }
        temp.next = node;
    }
}

public E getFirst() {
    return first.data;
}

public E find(E x) {
    if (first == null) {
        return null;
    } else {
        //while (temp != null) if node found set it's searched = true and move it to front of list
        Node<E> temp = first;
        while (temp != null) {
            if (temp.data.equals(x)) {
                temp.searched = true;
                break;
            }
            temp = temp.next;
        }
        if (temp == null) return null;

        pushForward(temp);
        return temp.data;
    }
}
//Find pre-linked node with our node, bind our node with parent next node
//and link parent with node.
private void pushForward(Node<E> node) {
    if (first == null || first.next == null) return;
    Node<E> temp = first;
    while (temp.next != null) {
        if (temp.next.equals(node)) {
            temp.next = temp.next.next;
            node.next = first;
            first = node;
            break;
        }
        temp = temp.next;
    }
}

private static class Node<E> {
    private E data;
    private boolean searched;
    private Node<E> next;

    private Node(E e) {
        data = e;
        searched = false;
        next = null;
    }
}

}

Also you could mix pushForward and find methods to make find to do what you want by one iteration through the list (O(n)), because O(n^2) there. Might be helpful: https://www.geeksforgeeks.org/java-program-for-inserting-node-in-the-middle-of-the-linked-list/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 danilllo19