Re: Double Linked Structure
Please use code tags when posting.
Quote:
this code right here needs to take advantage of a doubly linked structure
Where is this double linked structure you are supposed to be using and why doesn't the doubly linked structure handle adds, removes etc?
Quote:
it needs to be without the previous variable
That makes no sense to me and you explain what you are trying to do.
Re: Double Linked Structure
Well this is just a portion of a code, the idea is that the remove method instead of single linked now handles a double link, therefore it no longer needs a previous variable and needs to be able to jump straight from the contents variable to previous and next.
Re: Double Linked Structure
I know this is just a portion of the code but what code is it a portion of? It makes little sense on its own without a full explanation.
I still don't understand what you mean by no longer needing a previous variable. Singly linked structures have a single reference to another node, generally it is a reference to the next node in the structure (although there's no reason you couldn't do one with a previous reference instead), doubly linked structures have both a next and a previous reference hence the name doubly linked. How can you have a doubly linked structure without a previous reference?
Re: Double Linked Structure
@OP: Please use the [code][/code] tags to preserve indentation (it will not format unformatted code, though).
Otherwise, people will have a hard time reading your code and understanding your question!
@keang: He's referring to this declaration near the top of the remove() method - the local helper variable "previous":
DoubleNode<T> previous, current;
Basically, the code searches for the node that matches the remove target, simultaneously keeping track of the node it previously tested, so that, later on, it can do this:
previous.setNext(current.getNext());
@OP: Well, If you have a doubly linked list, you can simplify the code, because then the node class would have both getNext() and getPrevious(). Once you've found the target, you can simply do something like this:
DoubleNode<T> previous = current.getPrevious();
previous.setNext(current.getNext());
Or:
current.getPrevious().setNext(current.getNext());
The way you wanted to do it wouldn't work:
Quote:
contents.setPrevious(contents.getPrevious());
This doesn't change anything. It's saying "set my previous to my current previous"...
Re: Double Linked Structure
Quote:
Originally Posted by TheGreatCthulhu
@keang: He's referring to this declaration near the top of the remove() method - the local helper variable "previous":
DoubleNode<T> previous, current;
Basically, the code searches for the node that matches the remove target, simultaneously keeping track of the node it previously tested, so that, later on, it can do this:
previous.setNext(current.getNext());
Thanks for pointing that out.
If the OP can't be bothered to use code tags after being asked to do so then I generally don't bother reading their unformatted code.