Everything works fine right up until the last value of index 19. In fact, all values are printed and what not. Once it prints the final value & index, it seg faults. I am assuming this is because it is trying to access the 20th value. How would I prevent this from occurring?
MAIN FILE CODE:
int index = 0;
while (index < list.length())
{
cout << list.getNextItem(index) << " " << index << "\n";
index++;
}
HEADER CODE:
template <class Type>
Type doublyLinkedList<Type>::getNextItem(const Type& val) const
{
nodeType<Type> *current; //pointer to traverse the list
current = first; //set current to point to the first node
for (index=0; index < val; index++)
{
if (current != NULL)
{
current = current->next;
}
}
return current->info;
}//end getNextItem
getNextItemWhere isindexinitialized? Why is the list traversed like a poor man's vector?