again!
Now my issue is that I need to create a method lookup that will search a linked list in order to find a person and return that person back.
public Person lookup(String name) {
if(head == null) {
return null;
}
if(head.person.name.compareTo(name) == 0) {
head = head.next;
return person;
}
Node current = head;
Node prev = head;
while(current != null) {
if(current.person.name.compareTo(name) == 0) {
prev.next = current.next;
return person;
}
prev = current;
current = current.next;
}
return null;
}
Now the method takes this parameter name, compares the objects inside the list if there is a match it should return the person.The program with my code here is the return value; precisly the value it returns when the comparison equals 0.When I compile I get an error saying that it cannot find the symbol person.How do I tell the program to return the found person? Thanks!
You can find lookup by search node by node, like this:
public Person lookup(String name) {
if (head == null) { // check if head is null then Linkedlist is empty and return null
return null;
}
Node current = head; // start from head
while (current != null) {
if (current.person.name.equals(name)) { // if equals return person
return current.person;
}
current = current.next; // get the next
}
return null;
}