Results 1 to 2 of 2
  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    43
    Rep Power
    0

    Default Single Linked List boolean

    Hi i'm currently studying over single linkedLists and i'm just writing various methods for it. One method i'm currently stuck on is writing a remove method where it returns a boolean true if its found and it will delete it, and false if the target isn't found. My problem is that it always comes up as flase and i know that the target is in the linked list.
    Here is the code for my method.

    Java Code:
    public boolean remove(E item){
    Node<E> ptr = head;
    		while(ptr!= null && !item.equals(item))
    			ptr = ptr.next;
    		if(ptr.data.equals(item)){
    			ptr.next = ptr.next.next;
    			return true;
    		}
    		return false;	
    	}
    And here is the some of code that is in my testing class
    Java Code:
    Node<String> California = new Node<String>("California");
    		Node<String> Florida = new Node<String>("Florida");
    		Node<String> Alabama = new Node<String>("Alabama");
    		Node<String> Kentucky = new Node<String>("Kentucky");
    		Alabama.next = Florida;
    		Florida.next = California;
    		California.next = Kentucky;
    		
    		names.addFirst("Kentucky");
    		names.addFirst("california");
    		names.addFirst("florida");
    		names.addFirst("alabama");
    
    		System.out.println(names.remove("california"));
    		System.out.println(names.toString());

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Single Linked List boolean

    Do you really want to compare 'item' against itself in line #3?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. Replies: 0
    Last Post: 09-25-2012, 04:07 AM
  2. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  3. accessing single linked list from another class
    By el9335 in forum New To Java
    Replies: 4
    Last Post: 10-07-2010, 01:52 PM
  4. Linked list inside a linked list
    By viperlasson in forum New To Java
    Replies: 5
    Last Post: 07-26-2010, 11:15 PM
  5. Single linked lists - help
    By Srcee in forum New To Java
    Replies: 10
    Last Post: 10-29-2009, 05:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •