|
[Java] Dual-linked List: swapping List elements => loop?!
#267410
05/23/09 18:31
05/23/09 18:31
|
Joined: Jul 2002
Posts: 4,436 Germany, Luebeck
Xarthor
OP
Expert
|
OP
Expert
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
|
Hi there, I got a small problem with a java method I've written. Its a swap method which should swap two elements of a dual linked list (meaning each element has a 'next' and a 'previous' link, thus you can cycle through the list in two directions). The problem is: The resulting list contains a cycle, thus there is no head element. The code is:
private void swap(List a, List b)
{
List temp = a;
if(a.previous != null)
a.previous.next = b;
if(a.next != null)
a.next.previous = b;
if(b.previous != null)
b.previous.next = a;
if(b.next != null)
b.next.previous = a;
a.next = b.next;
a.previous = b.previous;
b.next = temp.next;
b.previous = temp.previous;
}
Anyone has an idea what I'm doing wrong here? Thanks in advance for any hints!
|
|
|
Re: [Java] Dual-linked List: swapping List elements => loop?!
[Re: Xarthor]
#267436
05/23/09 23:58
05/23/09 23:58
|
Joined: Feb 2009
Posts: 2,154
Damocles_
Expert
|
Expert
Joined: Feb 2009
Posts: 2,154
|
Im not really shure about the Class Type "List", as the standard Java List does not have previous and next, so I suppose its a new class. a.next = b.next; a.previous = b.previous; b.next = temp.next;//whatever is in temp was changed //already by a.next = b.next; ! , as temp and a point to the same object b.previous = temp.previous;
How about that code, you just cross copy the objects (now matter what they are, or if they are null) private void swap(List a, List b) { Object aNext= a.next; Object aPrevious= a.previous;
a.next = b.next; a.previous = b.previous; b.next = aNext; b.previous = aPrevious; }
//I think you need to parse it also //Givent that next and previous are of the class type "list" //do like b.next = (List)aNext; etc.
|
|
|
Re: [Java] Dual-linked List: swapping List elements => loop?!
[Re: Damocles_]
#267444
05/24/09 02:35
05/24/09 02:35
|
Joined: Dec 2008
Posts: 271
Saturnus
Member
|
Member
Joined: Dec 2008
Posts: 271
|
Hello! Perhaps the following pseudo code can help you too. I am using it for my own doubly linked list. However, it is assumed that there are already functions to cut and insert list elements. function: swap elements a and b
IF a is successor of b THEN
cut b and paste it after a
ELSE IF b is successor of a THEN
cut a and paste it after b
ELSE
remember predecessor of b
cut b and paste it before a
cut a and paste it after remembered predecessor of b
if b was the first element paste a at the front of list I dont know how your implementation works, so the above code my be completely incompatible with yours.^^
|
|
|
Re: [Java] Dual-linked List: swapping List elements => loop?!
[Re: Xarthor]
#267479
05/24/09 09:28
05/24/09 09:28
|
Joined: Feb 2009
Posts: 2,154
Damocles_
Expert
|
Expert
Joined: Feb 2009
Posts: 2,154
|
as a tip, you can see pretty deep into the java classes actual source code. I for example needed once a code for determining crosspoints between lines, and found it directly in the sourcecode from a Javaclass. Here the LinkedList for example: http://www.docjar.com/html/api/java/util/LinkedList.java.html
|
|
|
|