3 Replies - 20100 Views - Last Post: 19 April 2013 - 09:31 AM Rate Topic: -----

#1 LakersFan805   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

LinkedList.contains Method implementation always returns false

Posted 18 April 2013 - 10:24 PM

Just finished implementing the LinkedList, but i'm having a problem with the contains method; the problem is that whenever i call the function it always returns false.Somebody please help me.

//Return true if the given element exists within the list and false otherwise.
	public boolean contains(E element)
	{
		Node<E> cursor = head;
		
		for(int x = 0; x < size; x++)
		{
			if ( cursor == null )
			{
				return false;
			}
			else if (cursor.content == element)
			{
				return true;
			}
			else 
			{
				cursor = cursor.next;		
			}
		}
		return false;	
	}













Is This A Good Question/Topic? 0
  • +

Replies To: LinkedList.contains Method implementation always returns false

#2 ChrisGulddahl   User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 18
  • Joined: 17-April 13

Re: LinkedList.contains Method implementation always returns false

Posted 18 April 2013 - 11:33 PM

This code seems correct.
To know what's up I need more code
  • The code where you call the contains() method on some LinkedList.
  • The implementation of the method that inserts into the LinkedList.


Also you should consider using
else if (cursor.content.equals(element))

in line 12. By default this does the same as "==", but allows people to decide (by overwriting the equals() method) how to compare objects (you included - it may be the case that you're comparing objects that have the same state data, but have different location in memory and thus will not trigger '=='.
Was This Post Helpful? 1
  • +
  • -

#3 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: LinkedList.contains Method implementation always returns false

Posted 19 April 2013 - 03:52 AM

The code is kind of complicated for nothing and shows that you have a total lack of confidence in your own code.

If you maintain the size of your LinkedList in an instance variable why do you test in the loop id the Node is null ? It just can't !!!

As mentionned by ChrisGulddahl you can't compare 2 objects, like E, using the == operator so your code could be

Using the for() loop and the size
	public boolean contains(E element)
	{
		Node<E> cursor = head;
		
		for(int x = 0; x < size; x++)
		{
			if (cursor.content.equals(element))
				return true;
			
			cursor = cursor.next;		
		}
		return false;	
	}


or using the forward pointer which is a more common practice
	public boolean contains(E element)
	{
                Node<E> cursor = head;

		while(cursor != null)
                   if(cursor.content.equals(element))
                      return true;
                   cursor = cursor.next;
                }
		return false;	
	}



Happy coding

This post has been edited by pbl: 19 April 2013 - 09:39 AM
Reason for edit:: Remove a useless null test on the for()

Was This Post Helpful? 1
  • +
  • -

#4 LakersFan805   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 15-February 13

Re: LinkedList.contains Method implementation always returns false

Posted 19 April 2013 - 09:31 AM

View Postpbl, on 19 April 2013 - 03:52 AM, said:

The code is kind of complicated for nothing and shows that you have a total lack of confidence in your own code.

If you maintain the size of your LinkedList in an instance variable why do you test in the loop id the Node is null ? It just can't !!!

As mentionned by ChrisGulddahl you can't compare 2 objects, like E, using the == operator so your code could be

Using the for() loop and the size
	public boolean contains(E element)
	{
		Node<E> cursor = head;
		
		for(int x = 0; x < size; x++)
		{
			if (cursor.content.equals(element))
				return true;
			
			cursor = cursor.next;		
		}
		return false;	
	}


or using the forward pointer which is a more common practice
	public boolean contains(E element)
	{
                Node<E> cursor = head;

		while(cursor != null)
                   if(cursor.content.equals(element))
                      return true;
                   cursor = cursor.next;
                }
		return false;	
	}



Happy coding


Thanks a lot!

This post has been edited by pbl: 19 April 2013 - 09:40 AM
Reason for edit:: Removed a useless test for null in the for()

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1