• Post Reply Bookmark Topic Watch Topic
  • New Topic

Implementing a Linked List in Java (Data Structures & Algorithms)  RSS feed

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, I'm running into a problem attempting to implement a Linked List in java. I'm a bit stumped at this point. I'm taking an online MOOC and from the looks of it, I'm attempting to implement a get method that returns the matching node at a particular instance. This particular Linked List is a doubly linked list, thus it has two sentinel/dummy nodes (a head and tail).

When I attempt to return a matching node, I get "type mismatch, can not convert from LLNode<E> to E"  in this case, E is a generic type. Any input would be greatly appreciated of what I can do better. Thanks!

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm unable to edit my original post, but the method I need help with is the " public E get(int index)" method. at line 49
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the error message says it all really. Your get() method has to return an E, but matchingNode is an LLNode<E>.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"type mismatch, can not convert from LLNode<E> to E" i

Can you post the full text of the error message that shows what source line it was on and what the line number is for that source line?
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
"type mismatch, can not convert from LLNode<E> to E" i

Can you post the full text of the error message that shows what source line it was on and what the line number is for that source line?


Line 61 in the code posted here. Thanks!
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, the error message says it all really. Your get() method has to return an E, but matchingNode is an LLNode<E>.


I understand that, and you are completely correct Stephan, the only problem is that I want to return a node. However, I can't set a generic type to head.next since head is an LLNode object type. I'm a bit lost here.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to return a node

The comments for the get method say:  Get the element at position index /  That would be E not a Node.

If you want to return a Node, you need a new method with a definition like this:
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I want to return a node

The comments for the get method say:  Get the element at position index /  That would be E not a Node.

If you want to return a Node, you need a new method with a definition like this:


I know what I did wrong, I'm an idiot. I haven't tested it yet though so I'm not confident in my code until I have tested it. But I really am taking this MOOC as practice before I take the real thing at my University (taking Data Structures & Algorithms in Spring 2017). From my understanding, Data Structures & Algorithms is the most difficult course in the CS curriculum (that and Discrete Structures).

In any case, here is what I did differently since I'm a bit slow at times to understand stuff.



I will test it and report back promptly.
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That will work if all the nodes have the save data.  The code returns the data for the first node (line 9), not the ith node.

It needs to get  and return the data for the ith node.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:That will work if all the nodes have the save data.  The code returns the data for the first node (line 9), not the ith node.

It needs to get  and return the data for the ith node.


That's true I'll work on that and report back promptly.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:That will work if all the nodes have the save data.  The code returns the data for the first node (line 9), not the ith node.

It needs to get  and return the data for the ith node.


Hmm Norm, what am I doing wrong? My brain has turned into mush.



I increment the i so that by the time it reaches index, it prints the element at index (the ith element). However, it doesn't seem to be working. Sorry for the trouble. Thanks once again kindly!
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the code gets to the ith node, return its data.

No need to get the data for any other node.

Do you have a testing program?  What happens when  you execute it?
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:When the code gets to the ith node, return its data.

No need to get the data for any other node.

Do you have a testing program?  What happens when  you execute it?


Yep I do have a testing program (courtesy of the University of California, San Diego). When I run the Junit test, it fails at this line within the testing program:



When you say return the data at its "ith node", I thought that was what I was doing. Forgive me as I'm very tired. I am telling the program to go the next node while i is less than the specified user index, then at the end I increment i by 1.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to spend some time debugging the code to see what it is doing so you can fix it.
For that you need a testing program:  a main() method that defines an instance of the class, calls its add method a few times and prints out the contents of the list so you can visually verify it.

One technique I use for debugging singly linked lists is to add a toString() method to the node class that returns the value of the node and the value of the next link.
Then a simple print of the node at the head of the list will cause the whole list to be traversed and shown in the print out.
There is a problem with doubly linked lists that causes an infinite loop and stack overflow so only return the next value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!