Junilu Lacar wrote:
I don't know why you need N at all. You already initialize the bucket array to that length in the constructor so you can always use bucket.length where you're using N now.
Anyway, new StringBuilder(N) doesn't work the way you seem to think it works. myHashMap.length() will still be 0 so your for-loop won't even execute, not even once.
You chose a very poor name in myHashMap. First of all, that's a StringBuilder, not a HashMap. It's like declaring this:
What exactly do you want to iterate over anyway with that loop? Isn't it the bucket array?
Junilu Lacar wrote:My bad. I thought you were using a java.util.LinkedList, which also has an addLast() method but takes only one parameter.
Junilu Lacar wrote:Does line 83 even compile?
As far as I can tell, addLast() only takes one argument: https://docs.oracle.com/javase/10/docs/api/java/util/LinkedList.html#addLast(E)
Your code attempts to pass in two arguments.
Campbell Ritchie wrote:Welcome to the Ranch
Thank you for using the code tags, but you didn't quite get them right (see this link): don't worry, I can correct that for you since you are new.
Remember the structure of a linked list.
Current node ≡ nextNode.prev
nextNode ≡ Current node.next
So we assume you already have the appropriate place marked by having a ”live” reference to the current node. What you need to do is add a new node such that:-
Current node.next ≡ newNode newNode.next ≡ (old)nextNode newNode.prev ≡ Current node (old)nextNode.prev ≡ newNodeDon't try methods out in a large class. Create a small class that has the requisite features and try it out in that first. You can do that with two old nodes and one new node to insert between them. Once you have done that, you can copy the technique into your larger class.
I don't think what you are doing is going to work. Find the place to insert a new value before you create the new node. The new node should point to its two neighbouring nodes, not the end nodes. Then you can make the neighbouring modes point to the new node. Remember that if you get to the end node, its next field will point to null, so you cannot find a prev field to point to the new node.
I think maybe you should make the node class private.
All the above applies to an addBefore method, except that you look for end nodes the other way and prev might point to null.