I am trying to overload the << operator so i can output the contents of a linkedlist.
So if I have a linkedlist with the values 1, 2, 3 and I call:
cout << list << endl;
where list is of type LinkedList. I should get something like:
1 2 3 or 1 | 2 | 3 in the terminal.
Should i be using a loop to cycle through the list and add the data from each node to outs? Trying to implement a method that doesnt need to see the node class...
ostream& operator <<(ostream& outs, const LinkedList& source)
{
//use a loop to cycle through, adding each data to outs, seperated by a space or a |
return outs;
}
Would a structure like this work out alright?:
LinkedList::report(ostream& outs) //thanks beta :)
{
Node *currentPtr = headPtr;
while(currentPtr != NULL)
{
outs << currentPtr->getData() << " "; //thanks Rob :)
currentPtr = currentPtr->getNextPtr();
}
return outs;
}