Game Development Reference
In-Depth Information
ListNode* GetLast()
{
return m_Last;
}
void SetNext(ListNode* next)
{
m_Next = next;
}
ListNode* GetNext()
{
return m_Next;
}
};
Listing 15-1 represents a very basic implementation of a double linked list. A single linked list would
only have pointers to the next element in the list, but our ListNode class has pointers to both the
next and the previous nodes. ListNode also stores a void* to the data that the node represents.
A better way to implement this would have been to create our own templates, and I'll begin to cover
how to do this in Part 4 of this topic. You'll get a better appreciation of how the class operates by
looking at some code that puts it to use, such as in Listing 15-2.
Listing 15-2. Using ListNode
void OurList()
{
unsigned int firstData = 1;
ListNode first(&firstData);
unsigned int secondData = 2;
ListNode second(&secondData);
unsigned int thirdData = 3;
ListNode third(&thirdData);
first.SetNext(&second);
second.SetLast(&first);
second.SetNext(&third);
third.SetLast(&third);
for (ListNode* iter = &first; iter != nullptr; iter = iter->GetNext())
{
unsigned int* number = static_cast<unsigned int*>(iter->GetData());
cout << *number << endl;
}
}
 
Search WWH ::




Custom Search