<分区>
我的教授给了我以下声明,我需要根据 LinkedList 的这个声明编写几个函数,但我仍然无法确定几行代码的作用。我几乎了解这几行;
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;
}
这里是完整的代码。
#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;}
附言。有人也可以解释一下 friend operator 的作用吗,因为我以前从未使用过它?