|
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:
node(A)
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) |