简体   繁体   English

从LinkedList递归查找和删除节点

[英]Recursively Find and Remove Node from LinkedList

Given a string to search for, I want to write a recursive function that takes in only one parameter (The string to search for). 给定要搜索的字符串,我想编写一个仅包含一个参数(要搜索的字符串)的递归函数。 The function will search for the value recursively and if it is found then it will remove the item and return it. 该函数将递归搜索该值,如果找到该值,它将删除该项目并返回它。 If it is not found then, the function will reach the end of the list and return null. 如果未找到,则该函数将到达列表的末尾并返回null。 What I have so far I think is the right idea except it is not functioning properly: 到目前为止,我认为是正确的主意,只是它无法正常运行:

Main Test Class 主要考试班

public static void main(String[] args) {
    RecLinkedList list = new RecLinkedList();
    list.add("A");
    list.add("B");
    list.add("D");
    list.add("C", 2);
    list.add("E", 4);
    list.add("G", 6); //this should be invalid

    System.out.println( list );
    System.out.println( list.remove( 1 ).getValue() );
    System.out.println( list.remove("D").getValue() );
    System.out.println( list.remove("G").getValue() );
    System.out.println( list.size() );
    System.out.println( list );
}

Linked List Class (Showing only what I need help on) 链表类(仅显示我需要的帮助)

public class RecLinkedList {
private Node first;
private int size = 0;

public RecLinkedList(){
    first = null;
}
public boolean isEmpty() {
    return first == null;
}
public Node remove( String s ){
    return remove( s, 0, first );
}
private Node remove( String s, int count, Node list ){
    if( list == null ){
        return null;
    }else if( s.equals(s) ){
        first = list.getNext();
        return list;
    }else if( s.equals(count+1) ){
        Node n = list.getNext();
        if( list.getNext() != null ){
            list.setNext( list.getNext().getNext() );
        }
        return n;
    }else{
        return remove( s, count+1, list.getNext() );
    }
}

So far, I am able to remove the item but as of now the item "A" is getting removed when it should not be. 到目前为止,我已经可以删除该项目,但是到目前为止,当不应该删除该项目时,“ A”项目将被删除。 The final list should be A,C,E. 最终列表应为A,C,E。 (G should return and print null because it does not exist). (G应该返回并打印null,因为它不存在)。 I think I am close, but off by something minor, but I can not seem to figure it out. 我想我很亲密,但是有些小事,但是我似乎无法弄清楚。

There are several errors in your code (see comments below) : 您的代码中有几个错误(请参见下面的注释):

private Node remove( String s, int count, Node list ){
    if( list == null ){
        return null;
    }else if( s.equals(s) ){ // comparing s to itself means you always remove
                             // the first element from the list (since this
                             // condition is always true)
        first = list.getNext();
        return list;
    }else if( s.equals(count+1) ){ // comparing the String s to an int - makes
                                   // no sense, will never be true
        Node n = list.getNext();
        if( list.getNext() != null ){
            list.setNext( list.getNext().getNext() );
        }
        return n;
    }else{
        return remove( s, count+1, list.getNext() );
    }
}

It seems to me like there is some ambiguity in your question. 在我看来,您的问题有点模棱两可。 I understand that your method should search for an element, remove it if present, and return the same object. 我了解您的方法应该搜索一个元素,将其删除(如果存在),然后返回相同的对象。 If the element is not present, the method should return null. 如果该元素不存在,则该方法应返回null。 That seems pretty straight-forward since most of your utility methods are already implemented in LinkedList . 这似乎很简单,因为大多数实用程序方法已在LinkedList实现。 I thus recommend to extend that class: 因此,我建议扩展该类:

public class RecLinkedList<E>
    extends LinkedList<E>
{
    public E removeAndReturn(E element)
    {
        E result;
        if (this.contains(element)) {
            remove(element);
            result = element;
        }
        else {
            result = null;
        }
        return result;
    }
}

I don't see why you would want to implement this recursively. 我不明白为什么您要递归地实现这一点。

This could clearly be written more concisely, but the explicit if-else should make it clearer. 显然,这可以写得更简洁一些,但是显式的if-else应该使它更清晰。

EDIT : The more concise and probably better implementation would be: 编辑 :更简洁,可能更好的实现是:

public E removeAndReturn(E element)
{
    return remove(element) ? element : null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM