Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (Ayumi, 1 invisible), 584 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
[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 Offline OP
Expert
Xarthor  Offline 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:
Code:
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_ Offline
Expert
Damocles_  Offline
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.


Quote:
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)

Quote:

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 Offline
Member
Saturnus  Offline
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.

Code:
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: Saturnus] #267464
05/24/09 07:40
05/24/09 07:40
Joined: Aug 2003
Posts: 2,122
Berlin, Germany
checkbutton Offline

Expert
checkbutton  Offline

Expert

Joined: Aug 2003
Posts: 2,122
Berlin, Germany
Don't access the List-Elements directly, use the add, remove and get functions specified in the LinkedList Object.
http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html


I don't have a homepage, for god's sake!
Re: [Java] Dual-linked List: swapping List elements => loop?! [Re: checkbutton] #267469
05/24/09 08:15
05/24/09 08:15
Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
Xarthor Offline OP
Expert
Xarthor  Offline OP
Expert

Joined: Jul 2002
Posts: 4,436
Germany, Luebeck
Thank you everybody!

@Damocles:
Yes I use my own List implementation and what you mention did cross my mind this night. I'll test it right away.
Thank you!

@Kombucha: Thanks!

@Checkbutton: I could use the LinkedList Class of the java api, but I decided against it, as I could not find a way to really swap elements as most of the information of the LinkedList is hidden. But I'll check it out in the future.
Thank you.

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_ Offline
Expert
Damocles_  Offline
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


Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1