File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

moving a linked list into an array

 
James Novak
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I'm working on this new linked list program. Basically what I wanted to do was move all of the data from the linked list into an array, then have the array print out backwards. I was wondering how I would do that. Some one suggested using a loop to me to do that, but I find that method confusing still



This part below that creates the ListNode elements.


This is the driver.



 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're already looping over the list--the next part is moving each element to an array. What have you tried so far?
 
James Novak
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In all honesty I've just tried doing research because I have no clue as to how to really approach the problem.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd approach the problem by making an array and as I iterate over the linked list put each list item into the array.

Do you know how to declare an array?
 
Henry Wong
author
Marshal
Pie
Posts: 20828
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, this seems to be less of a linked list, and more like, some code that manipulate a bunch of nodes that happen to be linked together.

Maybe it would be a good idea to implement the linked list first. Have instances of your linked list class maintain the head, tail, and other needed data. Have methods that can be called to add, insert, and remove elements -- these methods should not expose the node class. Or maybe implement an iterator.

Once you have a fully functioning linked list class, creating a "to array" method should be straightforward.

Henry
 
James Novak
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what I have so far. I figure that I still need the cursor part of the program and I probably need to make it an array. What should I do from here?

NOTE: Am I accidentally making the entire linked list one item in the array?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47229
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. You are accidentally making a 1-element array which you promptly discard.
 
James Novak
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well how can I change my code to take all of the numbers and turn them into multiple elements.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create an array of the correct size, and save each list element in successive array elements.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic