0

i was given the following declaration by my professor and i have a couple functions i need to write based on this declaration of LinkedList but i am stuck on determining what a couple lines of code does. i understand everything pretty much up to these lines;

friend ostream& operator<<( ostream& os, const LinkedList &ll )
{
LinkedList::Node *current;
for (current = ll.head; current != NULL; current = current->next)
os << current->data << " ";
return os;
}

here is the complete code.

#include <iostream>
#include <cstdlib>
using namespace std;
class LinkedList
{
public:
LinkedList() { head = NULL; } // default constructor makes an empty list
// functions to aid in debugging
// -----------------------------
friend ostream& operator<<( ostream& os, const LinkedList &ll );
void insertHead( int item );
private:
class Node // inner class for a linked list node
{
public:
Node( int item, Node *n ) // constructor
int data; // the data item in a node
Node *next; // a pointer to the next node in the list
};
Node *head; // the head of the list
};
friend ostream& operator<<( ostream& os, const LinkedList &ll )
{
LinkedList::Node *current;
for (current = ll.head; current != NULL; current = current->next)
os << current->data << " ";
return os;
}
void LinkedList::insertHead( int item ) // insert at head of list
{
head = new Node( item, head );
}
LinkedList::Node::Node( int item, Node *n ) {Node::data = item; next = n;}

ps. can someone also please explain what friend operator does, coz i have never used it before?

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
zeDante
  • 53
  • 2
  • 5

1 Answers1

3

The line

friend ostream& operator<<( ostream& os, const LinkedList &ll )

overloads the "insertion operator", so you can display your list using the common C++ syntax:

std::cout << mylist;

The line above is equivalent to:

operator<<(std::cout, mylist)

The first argument is of type std::ostream, and the second one of type LikedList.

The operator needs to be a friend since it (probably) needs access to private/protected members.

See this for more details on operator overloading.

Community
  • 1
  • 1
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • @zeDante And for the other part of the code in question, you might want to read up on how a `for` loop works. See [this page](http://www.cprogramming.com/tutorial/c/lesson3.html) for some info on that subject. I'll leave the linked list portion of that line of code to you since the class subject appears to be linked lists. – Steve Feb 19 '15 at 00:26
  • thank you soo much this really makes alot more sense now. one final question just for clarification, do the line os< data << " " a way of saying 'cout' – zeDante Feb 19 '15 at 05:01
  • yes, `os` represents the input stream of the function, passed by reference. So when you say `cout << my_list`, `cout` is being passed to `operator<<` as the first argument. Inside the function it is referred as `os`. Note also that it is not necessarily to use `cout`, you can as well use a file `out_file << my_list`, and the content will be send to the file, since the latter is also an `ostream`. That's the beauty of using inheritance and writing a function that works for the base of a well designed hierarchy. – vsoftco Feb 19 '15 at 14:50