Sign in to follow this  
The Thing

Linked lists in C++ - how would I manipulate the elements of the list:

Recommended Posts

Hello Again, I have written a very simple puzzle game in Java which used a linked list to handle the logic behind the game - I was able to rotate the elements of the list clockwise or anti-clockwise in the list by using addFirst(), addLast(), removeFirst(), and removeLast(). Example: //Clockwise. myLinkedList.addFirst(myLinkedList.removeLast()); //Anti-clockwise. myLinkedList.addLast(myLinkedList.removeFirst()); I would like to re-write the game in C++ firstly as a console app, and afterwards enhance it using SDL for the graphics. My question therefore is how would I go about manipulating the elements of a linked list in that manner using C++? Regards, The Thing.

Share this post


Link to post
Share on other sites
Thing,

That depends on which API you are going to use. Specifically, do you plan to work with the Standard Template Library (STL), the .NET Framework Library, or something else?

For reference purposes, this would be the equivelant using STL:

std::list<SomeDataType> myLinkedList;

//Clockwise.
myLinkedList.push_front(myLinkedList.pop_back());

//Anti-clockwise.
myLinkedList.push_back(myLinkedList.pop_front());

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
have a look at the Standard Template Library

Share this post


Link to post
Share on other sites
If you write your own linked list then you can call them roughly the same way that you where calling them before.

theTroll

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
If you write your own linked list then you can call them roughly the same way that you where calling them before.

Among other things, you'll also have to worry about memory management. It's not worth it to roll your own when there are evaluated, tested and debugged implementations out there for free, IMO.

Share this post


Link to post
Share on other sites
// clockwise
myLinkedList.splice(myLinkedList.begin(), myLinkedList, myLinkedList.end() - 1);
// anticlockwise
myLinkedList.splice(myLinkedList.end(), myLinkedList, myLinkedList.begin());


Should work I believe although I don't presently have access to my compilers or my copy of the final draft standard to double check.

Σnigma

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