Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Printing out a linked list

 
Josh Theisen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys, I'm back again!

Alright, here's what I've got going on: I've got to add items (integers) to a linked list. I'll keep adding integers until I feel I have enough in there.
When -1 is entered, the loop stops and I need to then print out the linked list.

So far I've gotten the input part down just fine (I'll still have you take a look at it since I'd like feedback on how I decided to do it) but it's the print that I'm having issues with.

Here's what I've got:

Declares a new LinkedList


Asks for input until -1 is entered.


Now I'd like to create a print method... something that will print out what the list has in it.
Note that I'd like to just get some ideas of how to get the ball rolling with this method...I'm not a fan of just getting
the answer and pasting that into my program.

Thanks in advance!
 
Paul Clapham
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First investigate whether the toString() method of LinkedList already does what you want. If it's okay, then just use that.

Otherwise just iterate through the list and print each of the elements.

Of course it would help if you had some idea of what you wanted to print, before you started to write any code.
 
Josh Theisen
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any reason why it'd be bad to approach it like this? The reason it's named "unsorted" is because I'll have to do another method in which linked list is sorted...


Also, ignore the fancy system out ;)
 
Campbell Ritchie
Sheriff
Posts: 48402
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:
  • You ought to print something like " ... -1 to exit."
  • You do not have a method there. You have a method which fills a list, and a method which prints it, and you are pretending both are the same method. You ought to have two methods.
  •  
    Josh Theisen
    Ranch Hand
    Posts: 31
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the replies - I"ll be posting another thread sometime regarding a different question I have. But this thread can be locked/ignored as it was a dumb thread anyway.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic