4 Replies - 11335 Views - Last Post: 06 April 2007 - 10:29 AM Rate Topic: -----

#1 system0utprint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 07

Finding length of linked list

Post icon  Posted 05 April 2007 - 06:05 PM

I'm supposed to find the length of a linked list, made of Node objects. Each node stores an integer and a pointer to the next node (or null). I already tested this method on a sample linked list with integers...

public int length(){
		int counter = 0;
		Node temp = head;
		while(temp!=null){ 
		temp = temp.getNext();
		counter++;
		}


So if I say System.out.println((whatever).length());, it's supposed to print out a number, but my program always prints out a 0 no matter what. Apparently something's wrong but I'm not sure what...any help is very much appreciated! Thank you! :)

Is This A Good Question/Topic? 0
  • +

Replies To: Finding length of linked list

#2 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Finding length of linked list

Posted 05 April 2007 - 06:08 PM

you'll need to post the whole method, where does the value head come from?
Is head's next value == null maybe?
Was This Post Helpful? 0
  • +
  • -

#3 system0utprint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 07

Re: Finding length of linked list

Posted 05 April 2007 - 06:48 PM

Hi!
Here's the class with constructors, etc... I think that head points to the first node in the list. So in the method I created a temp that started at the first node in the list and moved along the list, keeping an incrementing counter. But it doesn't work... :unsure:

Thanks for any help! I'll post more code if necessary.

//here's part of the class
public class LinkedList{
	
	private Node head;
	
	/** Default constructor: sets head to null **/
	public LinkedList(){
		head = null;
	}

	/** num of nodes in list **/
	public int length(){
		Node temp = head;
		int counter = 0;
		while(temp!=null){
		temp = temp.getNext();
		counter++;
		}
	return counter;
	}
}


This post has been edited by system0utprint: 05 April 2007 - 06:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 204
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: Finding length of linked list

Posted 05 April 2007 - 06:52 PM

assuming that the method getNext() works properly, the code should work jsut fine. My guess is that the list created (there is a list created?) is empy or is only one element.
Was This Post Helpful? 0
  • +
  • -

#5 system0utprint  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 29-January 07

Re: Finding length of linked list

Posted 06 April 2007 - 10:29 AM

Hi, thanks for the help! :) I'll go check the list to see what's wrong.

This post has been edited by system0utprint: 06 April 2007 - 10:30 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1