简体   繁体   English

链表C ++节点构造函数

[英]Linked List c++ node constructor

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; 我也不明白为什么在此旁边有一组方括号... {head = NULL; } }

But most of all I don't understand this line of code... friend ostream& operator<<( ostream& os, const LinkedList &ll ); 但最重要的是,我最不理解这行代码...朋友ostream&运算符<<(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;}

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. 但是编译器会生成默认的默认构造函数,并且默认初始化(uff!)所有成员。 However head is a pointer which has no default initialization. 但是head是没有默认初始化的指针。 Without head = NULL , its value would be indeterminate. 没有head = NULL ,其值将是不确定的。 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 ); 但最重要的是,我最不理解这行代码...朋友ostream&运算符<<(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: 按照惯例,当按以下方式将数据发送到输出流(从std::ostream派生)时,将使用该运算符:

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; 按照此约定,该运算符返回与它的第一个参数( ostream& )相同的引用,以便可以如上所示进行链接。 the return value of the first operator call ( cout << list ) is used as the first argument of the second operator call ( [returned value] << endl ). 第一个操作员调用的返回值( cout << list )用作第二个操作员调用的第一个参数( [returned value] << endl )。

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. default constructor是可以不带参数调用的default constructor函数。 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. 该类中的此代码允许operator<<成为它的朋友。 Friend methods are used when a function requires access to protected or private members of a class. 当函数需要访问类的protected成员或private成员时,将使用Friend方法。

Since the head is a private member of LinkedList , the friend keyword is used to allow the output operator << access to it. 因为head是的私有成员LinkedListfriend关键字用于允许输出操作符<<访问它。 The operator << , in this case, allows you to write the class to output data streams, most notably std::cout . 在这种情况下,运算符<<使您可以编写类以输出数据流,最著名的是std::cout Take the following example: 请看以下示例:

LinkedList list;
std::cout << list << std::endl;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM