Jump to content
  • Advertisement
Sign in to follow this  
The Thing

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

This topic is 4528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!