2

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;
}
Cheeseman
  • 27
  • 1
  • 7

2 Answers2

7

I'd suggest this:

ostream& operator <<(ostream& outs, const LinkedList& source)
{
    source.report(outs);
    return outs;
}

This method doesn't see the node class. You keep your implementation in LinkedList::report(ostream& outs) -- where you use a loop.

EDIT:
A couple of corrections to your report method:

// It must have a return type (e.g. void), and it must be const because your
// operator takes a const argument, which must call this method
void LinkedList::report(ostream& outs) const 
{
    const Node *currentPtr = headPtr; // might as well make this const too

    while(currentPtr != NULL)
    {
        outs << currentPtr->getData() << " "; // you can use "|" if you want
        currentPtr = currentPtr->getNextPtr();
    }

    // no need to return anything
}
Beta
  • 96,650
  • 16
  • 149
  • 150
  • Tried to add a potential loop here, added it to your post instead, did not know you could edit other peoples posts :P – Cheeseman Sep 05 '11 at 14:31
3

Yes, you should implement a loop to iterate through your LinkedList class.

Since you don't show how LinkedList works, I can't tell you precisely what goes in your loop, but generally it might look like this:

ostream& operator<<(ostream& outs, const LinkedList& source) {
    while( there_remains_data_to_print(source) ) {
        outs << get_the_next_datum(source) << " ";
    }
    return source;
}

For example, if your LinkedList has STL-style iterators:

ostream& operator<<(ostream& outs, const LinkedList& source) {
    LinkedList::iterator it = source.begin();
    while( it != source.end() ) {
        outs << *it << " ";
        ++it;
    }
    return source;
}

Or, for a traditional c-style linked list:

ostream& operator<<(ostream& outs, LinkedList source) {
    while( source ) {
        outs << source->datum << " ";
        source = source->next;
    }
    return source;
}
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • Alas, you produce an extraneous delimiter at the end, which the OP might not like (given that he wants ` | ` as a delimiter). – Kerrek SB Sep 05 '11 at 12:59
  • @Kerrek - Yeah, I thought about adding logic to avoid that, but left it "as an exercise to the reader." – Robᵩ Sep 05 '11 at 22:27