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 7 of 7

Thread: Deleting an element out of a linked list with boolean method

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

    Default Deleting an element out of a linked list with boolean method

    Hey, long time no see?

    Jokes aside, I need to implement a delete method now. It is boolean as well but it uses a diffrent parameter. I've tested the code, teh insert method works fine, but the delete doenst.After I try to delete a Person the size stays the same.

     
    public boolean delete(String name) {
    		Node p = head;
     
    		if( p == null) {
    			size--;
    			return true;
    		}else {
     
    			Node temp = p;
    			int comparison;
    			while(temp.next != null) {
    				comparison = temp.person.name.compareTo(name);
    				if(comparison == 0) {
    					temp.next = temp.next.next;
    					size--;
    					return true;
    				} else {
    					temp = temp.next;
     
    				}
    			}
    		}return false;
    	}

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Deleting an element out of a linked list with boolean method

    Time for some debugging. Add some print statements that print out the values of variables as they are used and to show the execution path.
    The print out will show you what the computer sees when the code is executed and help you find the problem.


    One problem I see is the size of an empty list is decreased by 1.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Deleting an element out of a linked list with boolean method

    Aight I did some tweaks and now I've got this;

     
    public boolean delete(String name) {
            if (head == null)
                return false;
            if (head.name.compareTo(name) == 0) {
                head = head.next;
                size--;
                return true;
            }
     
            Node current = head;
            Node prev = head;
            while (current != null) {
                if (current.name.compareTo(name) == 0) {
                    prev.next = current.next;
                    size--;
                    return true;
                }
                prev = current;
                current = current.next;
            }
        }
    And this error comes up, I think I've had it before

    PhoneBookList.java:48: error: cannot find symbol
    if (head.name.compareTo(name) == 0) {
    ^
    symbol: variable name
    location: variable head of type Node
    PhoneBookList.java:57: error: cannot find symbol
    if (current.name.compareTo(name) == 0) {
    ^
    symbol: variable name
    location: variable current of type Node
    2 errors

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Deleting an element out of a linked list with boolean method

    PhoneBookList.java:48: error: cannot find symbol
    if (head.name.compareTo(name) == 0) {
    ^
    symbol: variable name
    location: variable head of type Node
    The compiler can not find a member named name in the Node class (head)

    Yes, that look familiar. Remember name is in the Person class that is inside of the Node class.
    refToNode.refToPerson.name
    If you don't understand my answer, don't ignore it, ask a question.

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

    Wink Re: Deleting an element out of a linked list with boolean method

    Yep,I've had that fixed it works:

     
    public boolean delete(String name) {
            if (head == null)
                return false;
            if (head.person.name.compareTo(name) == 0) {
                head = head.next;
                size--;
                return true;
            }
     
            Node current = head;
            Node prev = head;
            while (current != null) {
                if (current.person.name.compareTo(name) == 0) {
                    prev.next = current.next;
                    size--;
                    return true;
                }
                prev = current;
                current = current.next;
            }
    		return false;
        }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Deleting an element out of a linked list with boolean method

    Is it working now?
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Deleting an element out of a linked list with boolean method

    Yea works just fine

Similar Threads

  1. [SOLVED] Singly Linked List - Sort Method
    By Sfrius in forum Collections and Generics
    Replies: 6
    Last Post: July 26th, 2014, 05:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  3. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  4. help with delete method of linked list
    By sonicjr in forum Collections and Generics
    Replies: 1
    Last Post: February 18th, 2012, 09:21 PM
  5. deleting all linked list elements using ADT
    By memo1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 25th, 2011, 02:01 PM