NOTE: I haven't been here for a while and I lose my signature picture. I doubt it, but I wonder if anyone has saved it...
Linked lists in C++ are simple to do once you get used to them. They do require some understanding of pointers, and that can be a bit scary to new programmers. It will also go into a bit of Structs, Inheritance, and Classes. It will help to know all of these, but I'm presenting more of a working template for you to look at, rather then a true tutorial. The following will show a quick real world example from a game I'm working on.
First, what is a linked list? You can think of it as a powerful data structure. It doesn't do anything on it's own, but is used to store more then one item. This items isn't just a number or character, but can be a struct or class. Note that if you know how many items you are going to store an array will serve you much better as it is easier to use. It also doesn't let you choose a specific item to access... you will have to go over the entire list every time you want to find a specific item (there are ways around this, but for now we won't discuss them). Well, now we know that they are for storing any number of arbitrary data items where we want to loop over the entire list!
...Like when you want to update enemies in a game! :) Here is the example:
class CEnemy
{
public:
int xPos, yPos;
CEnemy(int x, int y)
{
xPos = x; yPos = y;
}
void updatePosition(int vx, int vy)
{
xPos += vx; yPos += vy;
}
};
// This is a standard class that stores x, y for position, and updates the position with a function.
struct SNode
{
CEnemy *enemy;
SNode *next;
SNode(CEnemy *e, SNode *n)
{
enemy = e;
next = n;
}
~SNode()
{
delete enemy;
}
};
// The node is what the linked list will be based around. As you can see it stores an enemy, and a pointer to the next node in the list. The constructor sets the enemy and next node, while the deconstructor (~node) deletes the enemy. This is important to do, otherwise it will be wasting memory that we don't have access to.
SNode *node = new SNode(NULL, NULL);
// This is just a declaration of a node. Note that is give it a dummy head. What this means it that we create a blank node that sits at the front. This makes the working with the linked list easier.
void createEnemy()
{
node->next = new SNode(new CEnemy(5,5), node->next);
}
// Yes, adding a new enemy to the list is this simple! We create a new enemy and set the x, y position to 5, 5. We then point the new nodes next to the the current node's next. Then we bump this node into the list by setting node->next equal to the new node.
void modifyEnemy()
{
SNode n = node;
while(n->next != NULL)
{
n->next->enemy->updatePosition(1, 1);
n = n->next;
}
}
// Here we update the enemies position. Note how we create a new SNode. This is important, otherwise we will be modifying the global pointer and will lose our pointer to the front of the linked list. Then we loop while there is an enemy to update (n->next != NULL), and update the enemy! If we didn't create the dummy header for the linked list, there would be much more to this step.
void deleteEnemy()
{
SNode n = node;
while(n->next != NULL)
{
if(n->next->enemy->xPos > 100)
{
SNode temp = n->next;
n = n->next->next;
delete temp;
}
else
n = n->next;
}
)
// This deletes an enemy if the xPos is over 100, for example if it runs off the screen. To delete something from the linked list properly, we create a temp variable to point to the node we want to delete. Then advance the n variable by 2 nodes. We then delete the temp variable to get rid of the node. If we don't want to delete it, we just advance one node.
------
There you go! Adding to a list, looping through the list, and deleting from the list. Now, I know some of the explanations where a bit sparse so please ask questions/give comments. The first step is to look up pointers if you haven't. They often look tougher then they really are! The next step is to draw pictures like the one included on this post. Trust me... it REALLY helps if you draw pictures for code one step at a time. Helps for debugging as well! :)
If anyone wants to see a drawing of the steps, please post which function you would like to see done. Otherwise try to modify these template functions into a quick program. Hopefully linked lists won't be as scary anymore! :)
