Campbell Ritchie wrote:Another suggestion:
Write down the structure of your tree with some likely values attached to each node. Remember there are bound to be nulls all over the place. You should now have a little diagram.
Remove one of the nodes. Start with a leaf and see how that alters the structure of the tree. Then remove a non‑leaf node and see how the tree alters. Convert the changes on the diagram into instructions for how to remove a node, and then convert that lot to code.
Winston Gutkowski wrote:
Tarrell Fletcher wrote:I'm just trying to see if the remove method seems correct because I am trying to test the methods but I do not know how to pass a comparator as an argument. Ive tried doing like...
And that's most of your problem, I think: You're concentrating too much on coding, when what you need to do is take a step back and understand the problem.
Linked Lists are tricky and fiddly, and can be very hard to visualise when you're just looking at code.
My advice: Turn off your computer and draw some diagrams. A node can be drawn with a simple circle or box and an arrow for the "next" pointer(*). So maybe start out with two of those named 'head' and 'tail' and draw out all the stages needed for an "add first" and "add last", along with written instructions in English. Once you're happy with that, do the same thing for your 'peek' (get()) and 'pop' (retrieve()) methods (those are the "official" names for those functions).
The main thing is to make sure you really know the mechanics before you write another line of Java code; otherwise you're going to spend a lot of time "trying things out".
(*) - I actually often use a dot and an arrow: a dot on the edge of the box or circle to denote the pointer itself, and an arrow to show when it's actually pointing to something (because it can be null, which means "pointing to nothing").
Apart from your getLast and retrieveLast methods having confusing names, there is some poor style there. Look for a style guide which tells you what to use instead. (Hint: try here.)
Campbell Ritchie wrote:That sounds very peculiar
Tarrell Fletcher wrote: . . . I am to use the comparator in my remove method. But so far does this seem correct? . . .
Winston Gutkowski wrote:
Tarrell Fletcher wrote:I need to make a method called getSize() and the method just returns the value of the instance variable you use to keep track of size. But here's the catch....I'm not suppose to traverse the list...
OK, so how else do you think you might do it? (Tip: the answer is in your instructions, which as far as I can see you haven't followed - at least not yet).
Ok so I just reread it, it seems like it is telling me to make an global counter and then I guess increment it whenever something is added to it. And Im just calling that variable to see where the counter is at. And I guess if I do a remove method to make sure I decrement whenever something is deleted?