method to reverse doubly linked list

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 20607 Views - Last Post: 03 March 2011 - 07:20 PM Rate Topic: -----

#16 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Re: method to reverse doubly linked list

Posted 03 March 2011 - 06:40 PM

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.
Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

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.
head --> node --> node --> tail
tail --> node --> node --> head


Was This Post Helpful? 0
  • +
  • -

#18 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

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;
   }


Was This Post Helpful? 1
  • +
  • -

#19 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: method to reverse doubly linked list

Posted 03 March 2011 - 06:49 PM

Other than the use of static, pbl's code illustrates the point I was trying to convey. :)
Was This Post Helpful? 0
  • +
  • -

#20 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

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?
Was This Post Helpful? 0
  • +
  • -

#21 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: method to reverse doubly linked list

Posted 03 March 2011 - 07:11 PM

View Postmacosxnerd101, 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
   }



View Post<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
Was This Post Helpful? 1
  • +
  • -

#22 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

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. :)
Was This Post Helpful? 1
  • +
  • -

#23 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: method to reverse doubly linked list

Posted 03 March 2011 - 07:20 PM

View Postmacosxnerd101, 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
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2