Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: lookup method in a sorted linked list

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default lookup method in a sorted linked list

    Okay this should be the final post about this God forbidden topic.So i need to implement this following method.

     
    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 idea behind this is to go through the list checking for the parameter name and if we find a match return the person with that name.I think I did the "travesing" or going through the list part corectly, but the issue is the return value.When nothing is found null; makes sence.But if we find the person we need to return it. The return person here doesn't work I get this error:
    PhoneBookList.java:92: error: cannot find symbol
    return person;
    ^
    symbol: variable person
    location: class PhoneBookList
    PhoneBookList.java:100: error: cannot find symbol
    return person;
    ^
    symbol: variable person
    location: class PhoneBookList
    2 errors

    Any help? Thanks


    EDIT: Okay I've figured it out actually,
     
    public Person lookup(String name) {
     
            if (head == null) { 
                return null;
            }
     
            Node current = head;  
            while (current != null) {
                if (current.person.name.compareTo(name)==0) { 
                    return current.person;
                }
                current = current.next; 
            }
            return null;
        }
    Should work
    Last edited by arhzz; May 16th, 2020 at 02:00 PM.

Similar Threads

  1. [SOLVED] Sorted list and compareTo method
    By arhzz in forum What's Wrong With My Code?
    Replies: 28
    Last Post: May 15th, 2020, 06:49 PM
  2. [SOLVED] Singly Linked List - Sort Method
    By Sfrius in forum Collections and Generics
    Replies: 6
    Last Post: July 26th, 2014, 05:21 PM
  3. help with delete method of linked list
    By sonicjr in forum Collections and Generics
    Replies: 1
    Last Post: February 18th, 2012, 09:21 PM
  4. Java Newbie - Sorted Linked List not inserting properly - please help!
    By bubbleboy in forum What's Wrong With My Code?
    Replies: 20
    Last Post: June 17th, 2011, 11:48 AM
  5. Sorted Doubly linked List
    By student in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2010, 09:52 PM