Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what does this method do to a linked list?  RSS feed

 
Martha Wg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am having trouble with method1(). what does method 1 do to a linked list? and how to test this method...? I am greatly appreciate your reply.


 
Pavan Kumar Dittakavi
Ranch Hand
Posts: 108
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say that this method1() , will reverse the linked list.

It is creating a new list and this new list will be equal to the original one after reversing!.

Thanks,
Pavan.
 
Martha Wg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you for your help. if the original list is {1,2,3,4}, after this method is called, the list would be {4,3,2,1},right?

I've got further questions about this method. I actually have no idea what the following questions talking about?

For a list with n nodes, what is the maximum number of nodes that are 'live' (i.e., accessible from a "root set" of variables) during the method method1(), and when does this maximum occur?
Give a simple modification of the method method1() above that minimizes the number of 'live' nodes that are necessary for the method to work, so that any node that will not be used later can be immediately reclaimed as garbage".
 
p ranson
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code doesn't work at all does it?
Do you want to know what 'method1' is supposed to do (assuming it worked), or what it actually does?

Doesn't the declaration on line 37 (Node start = new Node(22)) shadow the class variable on line 20? The method uses the class method which is not initialised, therefore you'd get a runtime exception.
Even in the method, you're doing something I don't understand. Declaring 'Node p = null' then passing it to the constructor while it's still null...

confused.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch both of you.

What happened when you tried to execute that method? Have you got some mthod which prints out the contents of the list?
Who wrote that dreadful line 42 with the two assignments in? Have you asked your teaching people to explain that question?

Have you tried to draw the data structure on paper? That is probably the most profitable approach at this juncture.

You ought to have written the two classes separately; with your poor indentation it looks as though TestNode is an inner class, but it is an ordinary top level class.
 
Martha Wg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martha Wg wrote:
I am having trouble with method1(). what does method 1 do to a linked list? and how to test this method...? I am greatly appreciate your reply.




Thank you very much. I did try to draw the data structure, thank you for your great tip. I now have a question about how to test the method1 which is no argument or no return type?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method1() indeed has no arguments or return value, but it does change the value of the variable start (in line 29). So, after calling the method, you could make your program have a look at the variable start and all the nodes that it links to.
 
Martha Wg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:method1() indeed has no arguments or return value, but it does change the value of the variable start (in line 29). So, after calling the method, you could make your program have a look at the variable start and all the nodes that it links to.


Thank you for your reply. I tried to test method1, but seems like the method does not execute. I didn't test the method in right way, did I?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calling the method two times, in line 13 and line 18. When you reverse the list two times, you get back the original list, so then it looks like nothing has changed.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!