• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Printing out a linked list

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ;)
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic