Im sorry my weak mind con not comprehend what you are saying to me. I am just looking for the easiest possible solution at this point, since my code is do in about 5 hours.
22 Replies - 20607 Views - Last Post: 03 March 2011 - 07:20 PM
#17
Re: method to reverse doubly linked list
Posted 03 March 2011 - 06:43 PM
Reversing the List along the Nodes is a viable solution as well, as pbl has suggested.
Basically, a double-Linked List looks like this. So normal order is from head to tail. Reverse order is from tail to head. Since your List is doubly-Linked, both models exist simultaneously. Therefore, it is easy to change direction.
Basically, a double-Linked List looks like this. So normal order is from head to tail. Reverse order is from tail to head. Since your List is doubly-Linked, both models exist simultaneously. Therefore, it is easy to change direction.
head --> node --> node --> tail tail --> node --> node --> head
#18
Re: method to reverse doubly linked list
Posted 03 March 2011 - 06:48 PM
class DList {
static boolean reverse = false;
DList getHead() {
if(reverse)
return tail;
else
return head;
}
#19
Re: method to reverse doubly linked list
Posted 03 March 2011 - 06:49 PM
#20
Re: method to reverse doubly linked list
Posted 03 March 2011 - 06:52 PM
Si just need to make head refer to tail and tail refer to head?
#21
Re: method to reverse doubly linked list
Posted 03 March 2011 - 07:11 PM
macosxnerd101, on 03 March 2011 - 08:49 PM, said:
Other than the use of static, pbl's code illustrates the point I was trying to convey. 
You need a single static variable shared to all instance of the Node so by setting that variable all Nodes will react reverse for next/previous
If not you will have to call setReverse(true) to all nodes
This trick used many times with comparable
class Person implements Comparable<Person> {
static boolean sortByName;
int compareTo(Person p) {
if(sortByName)
compare name
else
compare age
}
<3DIC, on 03 March 2011 - 08:52 PM, said:
Si just need to make head refer to tail and tail refer to head?
class DList {
static boolean reverse = false;
DList getTail() {
if(reverse)
return head;
else
return tail;
}
You will have to change getNext() and gerPrevious() too
#22
Re: method to reverse doubly linked list
Posted 03 March 2011 - 07:12 PM
An instance variable in the List class should be sufficient. You don't want to reverse all the Lists by having the static variable in the List class, and having a static variable in the Node class does the same thing.
#23
Re: method to reverse doubly linked list
Posted 03 March 2011 - 07:20 PM
macosxnerd101, on 03 March 2011 - 09:12 PM, said:
An instance variable in the List class should be sufficient. You don't want to reverse all the Lists by having the static variable in the List class, and having a static variable in the Node class does the same thing. 
May be, I would have to check the implementation

New Topic/Question
Reply




MultiQuote




|