This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding Tail Reference

 
Christopher Beech
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you find the tail reference of a linked list???

I'm sure that you would need to do a traversal. The tail reference would be the last node where the reference would be null.



Not sure what code to put after the traversal. Also, this isn't for homework.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm too lazy to check this, but how about...

[ March 28, 2006: Message edited by: Peter Chase ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems easier to read...

If you do this a lot and you can prove it's a bottleneck, can you modify your list to maintain a tail variable? I think that's fairly common.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, a tail reference in a linked list is common, and helps out performance a bit since a list is not random access.

If you don't/can't have one, traversing is your only option to get the data in the end of the linked list.
[ March 28, 2006: Message edited by: Rusty Shackleford ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic