简体   繁体   中英

Doubly Linked List Implementation and reusing iteration code

I have my own implementation of a doubly linked list. For each property inside of the node, I have a separate method to loop through the linked list. I'd like to have a single method that accepts a comparator in order to stop the iteration over the linked list. An example of how I'm visioning it is this: Pseudo-code: list.find((a, b) -> a == b) . This method will find one node, however what if I'd like to find multiple nodes? Rename findOneNode to nodeSearch(fn, bool stopAtFirst) and use overloading?

public ListNode searchAge(int age) {
    return findOneNode((Predicate<Person>) node -> node.getAge() == age);
}

public ListNode findOneNode(Predicate predicate) {
    ListNode current = header;

    while (current != null) {
        if (current.getPerson() != null) {
            Boolean found = predicate.test(current.getPerson());
            if (found){
                return current;
            }
        }
        // Reached trailer?
        current = (current.getNext() != null) ? current.getNext() : null;
    }
    return null;
}
  • Is the code above a good practice?
  • Is there a better way to implement this?
  • How would I, with good design, implement searching for one node, and multiple nodes in one method?

Clarifications

When I say find multiple nodes, I don't mean return all the nodes. It'd like to use the .toString() method on each node, store that into a StringBuffer ,and return a string.

If there are 4 attributes on the node object ( First Name, Last Name, Age, Middle Name ) I need 4 different methods and do the same looping operation until I find the requested Fname, Lname, Age, or Middle Name .

Well, the line current = current.getNext(); establishes the same result, and the Boolean found is superfluous, and could be in the test itself like if(predicate.test(current.getPerson()))

Depending on the desired genericity, your Predicate should probably be on the ListNode, instead of on the Person (you're mixing them up in variable names now)

As for better: it could also be done using recursion, which is usually a bit slower, but easier to understand (and if you're using Scala, it'll compile it to a for-loop).

As for searching for multiple nodes, that is something which is usually called a filter, which returns a collection of its own of the same type (so another linked list, with the same persons in it).

For each property inside of the node, I have a separate method to loop through the linked list.

Conceptually it's a bad design. It's not good cause it's not flexible. Even now you already have the requirement but you can't reach it.

In general, you should separate double list implementation from actual nodes payload ( Person entity or so). If you think think this way you will realized that you can use any exist java List<T> implementation. Eg LinkedList<Person> . So you can easily get nodes with persons fit in your predicate

List<Person> people = new LinkedList<>();
...
List<Person> sameAgePeople = people.stream()
    .filter(node -> node.getAge() == age)
    .collect(Collectors.toList());

As a result you will have new sameAgePeople list with people fit your criteria.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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