簡體   English   中英

在 LinkedList 中搜索並將值放在 Linkedlist 的開頭

[英]Search in LinkedList and put value to beginning of Linkedlist

我的任務是在LinkedList中搜索值,如果找到一個值,則應將其刪除並放置在LinkedList的開頭。

class ListItem { 
    String value;
    ListItem next; 
    ListItem previous;
 
    ListItem(String value) { 
 this.value = value;
    } 
} 
 
 
class LinkedList { 
    ListItem first; 
    ListItem last; 
 
    public boolean contains(String v)  
    { 
 /* MODIFY THIS CODE */ 
 ListItem current = first; 
 
 while (current != null) 
 { 
 if (current.value.equals(v)) { 
 return true; 
 } 
 current = current.next; 
 } 
 return false; 
    } 
 
    public void add(String v){ 
         
 if (this.first == null) { 
 this.first = new ListItem(v); 
 } else { 
 ListItem i = this.first; 
 while (i.next != null){ 
 i = i.next;
 } 
 i.next = new ListItem(v); 
 } 
    } 
} 
 
LinkedList ll = new LinkedList(); 
ll.add("a");
ll.add("b");
ll.add("c");
 

assert ll.contains("c"); 
assert ll.first.value.equals("c"); 
assert ll.contains("d") == false; 
assert ll.first.value.equals("c"); 

如果它可以提供一些幫助。

class Node<T>{
    T data;
    Node<T> next;

    Node(T data){
        this.data = data;
    }
}

class SearchInsert{

    public static Node<Integer> createLinkedList(){
        Node<Integer> n1 = new Node<>(10);
        Node<Integer> n2 = new Node<>(20);
        Node<Integer> n3 = new Node<>(30);
        Node<Integer> n4 = new Node<>(40);
        Node<Integer> n5 = new Node<>(50);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        return n1;
    }

    public static Node<Integer> searchAndInsertAtBegining(Node<Integer> head, int value){ // value is the data for which we search in the list and insert it at the begining
        int pos = 0;
        int found = 0;
        Node<Integer> tempHead = head;
        Node<Integer> prev = head;
        while(head != null){
            if(head.data == value){
                found++;
                break;
            }
            pos++;
            head = head.next;
        }

        if (found == 1) {
            if (pos == 0) {
                return tempHead;
            }
            else{
                Node<Integer> nodeToBeDeleted = head; //node which are inserted at the begining of the list was found in the above whoile loop.
                int count = 0;
                while(count < pos -1 && prev.next != null){ 
                    count++;
                    prev = prev.next;
                }
                if (prev.next != null) {
                    prev.next = nodeToBeDeleted.next;
                    nodeToBeDeleted.next  = tempHead;
                    return nodeToBeDeleted;
                }
            }
        }
        return null;
    }

    public static void printlist(Node<Integer> head){

            //print recursively
        if (head == null) {
            return;
        }
        System.out.print(head.data + " ");
        printlist(head.next);
    }

    public static void main(String[] args) {
        Node<Integer> output = searchAndInsertAtBegining(createLinkedList(), 50);
        printlist(output);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM