• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on insert method of a linked list  RSS feed

 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, welcome to the Ranch!

Is there a particular implementation of a linked list you are referring to in your question?
 
Ameena Rupam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm no. For a queue,

So I am just wondering, don't these nodes get destroyed after the method is executed? How do they stay in memory? Aren't those objects temporary local variables?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ameena Rupam wrote:So I am just wondering, don't these nodes get destroyed after the method is executed? How do they stay in memory? Aren't those objects temporary local variables?

Sure, but the variable is NOT the object; it's a reference to the object. So as long as something points to it, it won't be removed - and in your case it would appear that REAR continues to point to it after the method is complete, so: no problem.

Winston
 
Ameena Rupam
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FRONT points to the first node and REAR points to the new node added. So these two will not be destroyed because there are references to them. So you mean to say that all the other nodes in between them won't be destroyed because each one of them has the previous node's 'next' field pointing to them? Thanks for your help Tim and Winston!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ameena Rupam wrote:So you mean to say that all the other nodes in between them won't be destroyed because each one of them has the previous node's 'next' field pointing to them?

Pretty much. but if FRONT ever goes "out of scope" then the whole ball of wax will unravel.

Maybe not instantly, but eventually the gc will work out that there is nothing that points directly or indirectly to any of the Nodes, so it'll eventually remove all of them (except maybe REAR, if it's still in scope; but that seems unlikely).

HIH

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!