-1

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

2 Answers2

1

First of all I thought a default constructor was made by the program itself when there is no constructor written.

Well, default constructor is any constructor that can be called without arguments. But the compiler does produce default default constructor and that default-initializes (uff!) all members. However head is a pointer which has no default initialization. Without head = NULL, its value would be indeterminate. Still, a better way would be:

LinkedList() : head(NULL) {}

But most of all I don't understand this line of code... friend ostream& operator<<( ostream& os, const LinkedList &ll );

It defines operator << in global scope (outside the class) which is also a friend of the class. The same can be done with:

class LinkedList
{
friend ostream& operator<<( ostream& os, const LinkedList &ll );
};

ostream& operator<<( ostream& os, const LinkedList &ll )
{
    …
}

This operator is then used by convention when sending data to output streams (which are derived from std::ostream) in the following manner:

LinkedList list;
cout << list << endl; // Dumps the list to standard output

As per this convention, this operator returns the same reference it gets as its first parameter (ostream&) so that it can be chain-linked as shown above; the return value of the first operator call (cout << list) is used as the first argument of the second operator call ([returned value] << endl).

StenSoft
  • 9,369
  • 25
  • 30
  • Since he says he doesn't understand the `<<`, a bit more of an explanation on what it _does_ might not be a bad idea. – Mooing Duck Feb 09 '15 at 20:10
  • I though he didn't understand the syntax of inline definition of a friend method but I have added this as well. – StenSoft Feb 09 '15 at 20:14
  • Oh I see, thank you for your explanations. So is ostream& operator a name of a function.? This is where I am getting confused. I know that the friend keyword allows a function that is not part of the class to access its private and protected data members. I also know what the extraction operator is, but looking at this doesn't make sense to me. Why is there an ampersand after ostream? The parameters this function takes also are confusing me because I don't know what they are. I am sorry if this is too many questions. – shqiperia585 Feb 09 '15 at 20:30
  • The function is named `operator <<`. I have updated the description to explain the `ostream&`. – StenSoft Feb 09 '15 at 20:33
  • Thank you, I understand now. The only part remaining that I don't understand is why you do not set the value of head to null in the parentheses, like so LinkedList( head = NULL). Is this because if you write head = NULL in the parentheses it becomes a parameter? Therefore you must write it in the square brackets? Also, I know it is not important but I am female not male. Thank you. – shqiperia585 Feb 09 '15 at 20:43
1

First of all I thought a default constructor was made by the program itself when there is no constructor written.

A default constructor is one that can be called with no arguments. So, in this case, we have a user-defined default constructor. This is perfectly valid.

But most of all I don't understand this line of code...

friend ostream& operator<<( ostream& os, const LinkedList &ll ); This code in the class allows the operator<< to be it's friend. Friend methods are used when a function requires access to protected or private members of a class.

Since the head is a private member of LinkedList, the friend keyword is used to allow the output operator << access to it. The operator <<, in this case, allows you to write the class to output data streams, most notably std::cout. Take the following example:

LinkedList list;
std::cout << list << std::endl;
Tyler Jandreau
  • 4,245
  • 1
  • 22
  • 47