• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inserting/Linking nodes at the end  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am new to Java and i am having trouble trying to understand the insertion and removal of Nodes.

I have to insert a new node at the end of the linked nodes, if i insert by terminal : 1, 2, 3. The printed nodes are going to be in the same order, 1,2, 3.

I have done the exercise, but it only prints the last node created and i dont know where its the problem. We did it before with "insert at the beginning", may be the problem is with the print() method in SimpleList.java, but i dont know how to print "backwards".

This is the code:

Node.java



SimpleList.java




TestSimpleList.java
 
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Hernan.

What you are effectively doing each time you call insertAtTheEnd(int num) is replacing Node next with a new Node. All subsequent values are lost, which is why only the last value is showing.

You need to create a collection to store the 'forgotten' nodes so you can iterate through them to call infoNode. SimpleList at the moment isn't a list at all.

Hope it helps

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you
 
Ash Jon
Greenhorn
Posts: 16
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch both of you


Thanks Campbell Looks to be a nice community here
 
Hernán Ramírez
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ash Jon wrote:Hi Hernan.

What you are effectively doing each time you call insertAtTheEnd(int num) is replacing Node next with a new Node. All subsequent values are lost, which is why only the last value is showing.

You need to create a collection to store the 'forgotten' nodes so you can iterate through them to call infoNode. SimpleList at the moment isn't a list at all.

Hope it helps



Hi, can you help me how to get info about collections of nodes in internet ? We have done the exercise to print "Last In, First Printed", but i dont see a collection there.

All my searches send me to the features Java has as a languaje and not the logic i need to understand(I am not sure if i am translating well when doing the searches), they also send me to Trees and topics that are more advanced. I am OK with Arrays and with Matrices, but this theme is being really hard for me X_X

Campbell Ritchie wrote:Welcome to the Ranch both of you


Thanks, it's a nice place to learn
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java® has lots of different classes suitable for collections. You can read about them here. That might be what you want. If not, ask again.
 
Bartender
Posts: 1838
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are over complicating this.
If you are adding a new item to the end, then all of the existing links are fine. Why would you mess with them?

So my method of doing it would be
#1: Find the end of the list
#2: Put a new element at the end.

you should not be modifying the "head" of the list (thats the same as it was before)
The only "next" record that needs to be edited is the one for the record currently at the end of the list.

You definitely shouldn't need anything from the collections framework
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!