I was given this code to manipulate to practice Linked Lists, but before I start, there are a few things I don't understand. First of all I thought a default constructor was made by the program itself when there is no constructor written. Here there is a comment saying "default constructor makes an empty list" next to a written constructor. I also don't understand why there is a set of square brackets next to this... { head = NULL; }
But most of all I don't understand this line of code... friend ostream& operator<<( ostream& os, const LinkedList &ll );
What does this do?
#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 )
{
for (Node *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;}