• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Cuajin

Exchange the position of two Nodes [Java]

4 posts in this topic

I’m trying to exchange the position of two nodes a and b.

I have the Node class that helps me create a list.
[CODE]
class Node
{
E info;
Node next;

Node (E x)
{
next = null;
info = x;
}
}
[/CODE]

Then I have class List.
[CODE]
public class List<E>
{
private Node headNode;
// some methods like addToList(). They work.

// Problem here!
public void exchangeNodes(int a, int b)
{
Node lead = head;

while (lead != null)
{
// here I wrote a bunch of noob code that if you looked at it your eyes would hurt.
}
}
[/CODE]

TestClass
[CODE]
public class TestClass
{
List myList = new List();
// populating the list with strings.
myList.add("a");
myList.add("b");
myList.add("c");
myList.add("d");
myList.add("e");
myList.add("f");
myList.add("g");

myList.exchange(2, 5); // giving it position numbers to exchange.
}
[/CODE]

The big problem is I can't get it to switch the Node's position on the list without destroying Nodes (I lose the reference to pointers when I switch). Clearly Nodes and me don't like each other [img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img]

From the TestClass I want to say exchange what's on position 2 with what is on position 5.

Please help.
0

Share this post


Link to post
Share on other sites
If you want to numerically index a list like that, you need to start from the beginning of the list, incrementing a counter and moving to the Next node each time through the loop until the counter reaches the index value or the end of the list is reached (raising an out_of_bounds error). Once you have found the nodes for each of the given indices, it is a simple matter to swap the values of [b]info[/b] for the two nodes.

Is exchanging two nodes an operation that you are going to be performing frequently? If so, using a linked list could hurt your performance.

It is always beneficial to analyze the most common use-cases for your data structures, and choose structures appropriately. In this case, indexing the structure via a numerical index is an example of a random-access usage, and linked-lists are notoriously poor for random-access since, as you can see, you have to iterate through the list with each access. Whereas, with an array type, the random-access is a simple array indexing operation. Of course, you also have to take into account your other usage patterns, which might offset this disadvantage.
0

Share this post


Link to post
Share on other sites
I don't plan to use the exchangeNodes as much. I was originally thinking ArrayList but it was too slow for the other operations.

I could swap the info parts, but I actually need to swap the nodes (might also be good to learn to actually swap the nodes because I need a similar thing for another project, a game involving maple trees).

I am trying the trailer / leader approach to swap the nodes but I just ended up creating a queue during execution which is definitely bad since all I wanted was to swap nodes.
0

Share this post


Link to post
Share on other sites
First, there's nothing wrong with a bunch of "noob code that would make our eyes hurt". If you don't show us what you tried, we can't help you make it work. That said, off the top of my head you'd do something like:[list=1]
[*]Ensure that [i]a[/i] and [i]b[/i] are valid indexes. There is no point in traversing the list if the indexes are either wrong (< 0) or out of bounds (>= [i]list.size()[/i]).
[*]Traverse the list and determine where your nodes actually are. If you're not going to determine which of [i]a[/i] or [i]b[/i] is smaller ahead of time then you need to keep track of where you are in relation to the indexes you are searching for. Keep in mind that your placement should be the parent of the first node you need, while your temporary swap should be the node itself. Once you've found both nodes (remember, parents only) you can perform a simple swap and fix your links without needing to traverse again.
[list=1]
[*]You should end up with three variables: [i]firstParent, temp [/i](=[i] firstParent.next[/i]), and [i]secondParent[/i].
[/list] [*]Make sure you handle all the boundary cases for when either [i]a[/i] or [i]b[/i] indicate the head or the tail of your list, which will result in specific, faster implementations (for obvious reasons).
[/list]

Also, if you've already implemented [i]remove()[/i] and [i]insert(int)[/i] you can simply use those instead. Technically, that isn't particularly performant but if you're not going to use [i]exchangeNodes(int, int)[/i] all that frequently then it's something to consider.
0

Share this post


Link to post
Share on other sites
[quote]
I was originally thinking ArrayList but it was too slow for the other operations.
[/quote]
I highly doubt this. Linked lists look great in theory, but their memory overhead (which is particularly bad in Java) and their cache incoherency will almost always cost more than a contiguous dynamic array implementation.

[quote]
I could swap the info parts, but I actually need to swap the nodes
[/quote]
If your code depends on the difference, then it is extremely brittle. The existence of nodes in a linked list is an implementation detail that shouldn't be directly exposed. It is also harder to maintain such code. Finally, it involves more work and so is less efficient than just swapping the data references.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0