You’ll need to be able to work with groups or collections of items. A game that only has one character with one action that can be done and with one opponent isn’t going to win you any awards. You have choices and this episode continues more than a week long exploration of collection types available. You’ll learn when to use each type of collection and why. Up today is the list. I’ll explain what lists are and then give you some guidance on when to use them.
Let’s say you have three instances of a letter class that you want to add to a list. A single linked list would look like this with arrows pointing from one item to the next:
head -> node(A) -> node(B) -> node(C)
The list class has a head member pointer that points to the first node in the list. The nodes are classes also that are designed to work with the list class. Their job is to point to the instances of the classes you actually want in the list as well as point to the next node. While I wrote in this example:
think of this more like:
node -> A
it was just a bit confusing trying to draw two pointers in one line so I put the instances of the letter class in parenthesis.
If this was a double linked list, then it would look like this:
head -> node(A) <-> node(B) <-> node(c) <- tail Notice in this case how the head has a pointer to the first node but the first node does not point back to anything. It still has a pointer member variable. All node classes in a double linked list have two pointers, one called previous and another called next. The first node has no previous item so it's previous pointer is null. The first node does point to the second node and the second node points back to the first node. That's what I mean when I draw the arrow pointing in both directions.