链表的默认构造函数。 [英] Default constructor for a linked list.

查看:70
本文介绍了链表的默认构造函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实现LinkedList类的默认构造函数。请记住,默认构造函数的工作是创建一个空列表。



在这个版本的Node中,我们使用一个struct with member int data和Node * next 。





我的下面的功能不正确。你能帮忙解决这个问题吗?



我尝试过的事情:



 LinkedList :: LinkedList()
{
int data;
Node * next;
data = 0;
* next = NULL;

}

解决方案

可能它应该类似于

  class  LinkedList 
{
// 类的私有成员变量
int 数据;
Node * next;
// ...
public
// 构造函数:将对象置于已知状态
LinkedList():data( 0 ),next( nullptr ){}
// ...
};


您好会员13041326



此行错误:

 * next = NULL; 

应该是:

 next = NULL; 

next 是指向下一个节点的指针;你想在初始化(也就是构造函数)将它指向无(又名NULL)。



* next 将意味着存储在 next 的值。 next 是指针, * next 节点 next 指向)。错误的行试图将0(NULL为0)分配给节点类型,这是错误的,因此这肯定会产生编译错误(例如,node = 0是不可能的)。


Implement the default constructor for the LinkedList class . Remember, the default constructor 's job is to create an empty list.

In this version of Node, we are using a struct with members int data and Node *next.


My function below is incorrect. Can you please help fix this?

What I have tried:

LinkedList::LinkedList()
{
   int data;
   Node *next;
   data=0;
   *next = NULL;
	
}

解决方案

Probably it should be something similar to

class LinkedList
{
  // private member variables of the class 
  int data;
  Node *next;
  //...
public:
  // constructor: put the object in a known state
  LinkedList() : data(0), next(nullptr) {}
  //...
};


Hi Member 13041326,

This line is wrong:

*next = NULL;

It should be:

next = NULL;

next is a pointer to the next node; you want to point it to none (aka NULL) at the initialization (aka constructor).

*next would mean the value stored at next. next is a pointer, and *next is the node (that next is pointing at). The erroneous line is trying to assign 0 (NULL is 0) to a node type which is wrong, hence this would definitely give a compile error (e.g. node = 0 is not possible).


这篇关于链表的默认构造函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆