Alex Ba wrote:
That's what I have currently. Because it needs to be a doubly linked list, do I need to add in a previous pointer to the .next node? Something like:
Because in the insertion, the previous pointer is always null, which makes sense because it's adding to the integer to the first part of the list. I'm confused because then wouldn't the next value have a previous pointer of null?
Also, I was provided a delete method which isn't working. So I'm writing my own. What I have:
But when I try to use the method that I wrote, I get a nullpointer exception. Are there any obvious problems with my delete method?
Steve
Alex Ba wrote:
I think I'm getting the error because my previous pointers are all wrong. I can't figure out why though.
Steve
Alex Ba wrote:I got all that, but I don't understand the coding part. I went through multiple weeks of explaining the concepts behind linked lists, and roughly 30 seconds of code
Steve
Alex Ba wrote:So is the insert method even correct for the next pointer?
Steve
Steve
Alex Ba wrote:
I'm not entirely sure if it works, as I haven't really tested it yet.
And by doing it that way, the second item in the list is given a previous pointer, and already has a next pointer.
And the problem, I should make it:
or something of that nature?
Steve
Steve
Steve
Steve
Alex Ba wrote:Current = newnode.next doesn't set the next pointer of newnode to current?
This is the tentative insert method:
Steve
Steve
Alex Ba wrote:So here's the story. My compiler decided it wasn't going to run the edited version of the java file. It was going to run the file that I opened initially. For that I am kicking myself, as I've been editing and running it for the past 2 days, only to find the same output every damn time.
Anyway. I switched compilers and I got the nullpointerexception. Now I'm absolutely lost.
Steve
Alex Ba wrote:Anyway. I switched compilers and I got the nullpointerexception. Now I'm absolutely lost.
Alex Ba wrote:The concept part isn't the problem. I know that in a doubly linked list each node has a value, a pointer to the next node, and a pointer to the previous node. I know that when you add a new node, it is added to the head of the list. I know that the first node in the list has a previous value of null because it's the first, and I know that the last node in the list has a next value of null.
Alex Ba wrote:
*edit* I LIED. You're setting your new node's next pointer to the OLD head, since it hadn't been changed yet. And after you're setting the next to the old head, you switch the head to the new newnode. In regards to Steve's post, I only added one line, but I didn't edit any others. I feel as though I've missed something.
Steve
Alex Ba wrote:I can't find the problem with my print statement.
Steve
Alex Ba wrote:
And when there's 2 items in the list that are both supposed to be deleted, the first isn't and the second throws a nullpointerexception for the line. I don't understand why though.
Alex Ba wrote:But I don't understand, you're setting current to head, and that means current doesn't have a previous value, so previous would be null, no?
Steve
Consider Paul's rocket mass heater. |