Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链表,将节点添加到末尾_C++_List_Linked List - Fatal编程技术网

C++ 链表,将节点添加到末尾

C++ 链表,将节点添加到末尾,c++,list,linked-list,C++,List,Linked List,我正在做一个项目,我被赋予了完成这个功能 void addToEnd(node*& head, string newVal) Effect: adds new node to tail end of list Precondition: head is a pointer to first node in the list (list MAY be empty) Postcondition: list contains one more node 我的问题是字符串newVal是什

我正在做一个项目,我被赋予了完成这个功能

void addToEnd(node*& head, string newVal)

Effect:  adds new node to tail end of list 
Precondition: head is a pointer to first node in the list (list MAY be empty)
Postcondition: list contains one more node
我的问题是字符串newVal是什么意思?

这个类的value_类型是DOUBLE类型,所以我不知道字符串newVal是什么。所以我不能在节点中设置newVal,因为它有两种不同的类型

这就是我目前所拥有的。我不确定我走的方向是否正确

node *temp = new node;
temp = head;

while(temp->link() != NULL){
    temp = temp->link();
}

head->set_link(temp);
我甚至不知道在这段代码中在哪里使用字符串

link()
返回成员变量
node*link\u字段

set\u link()
将新链接设置到
link\u字段

好吧,我们猜测他们希望您使用类似的函数将字符串转换为双精度字符串

至于列表操作代码,有几个问题:

node *temp = new node;
temp = head;
这将创建一个新节点,将其指针放入
temp
,然后立即用
头覆盖
temp
,丢失(泄漏)新节点。不要那样做

while(temp->link() != NULL){
    temp = temp->link();
}
这很接近,但可能不起作用。问题是您需要跟踪实际节点指针,而不是副本

通常,在使用指针而不是引用的链表API中,“添加节点”函数如下所示:

void addToEnd(node** head, string newVal)
{
    while(*head)
        head = &((*head)->next);
    *head = new node;
    (*head)->value = newVal;
    (*head)->next = 0;
}
请注意,如果列表为空,则传入的头指针将更改为指向新节点。如果列表不是空的,则最后一个
next
指针将被更改

给定的API(即
链接
设置链接
方法)不允许这样做,因为头指针不是
节点
,这些函数需要
节点
。所以你必须做一些不同的事情,也就是你必须单独处理空列表的情况

void addToEnd(node*& head, string newVal)
{
    // Create the node.
    node* newNode = new node;
    newNode->value = std::stod(newVal);
    newNode->set_link(0);

    if(!head) // Empty list?
    {
        head = newNode;
        return;
    }

    // Find last node.
    node* item = head;
    while(item->link())
        item = item->link();
    item->set_link(newNode);
}

添加新节点时,将该新节点的值设置为stringval。另外,我认为你没有指定链接和设置链接做什么“这个类的value\u类型是DOUBLE类型”检查实际上可以做什么。哇,这是一个非常糟糕的规范。你只需要猜测
newVal
在前置条件中应该扮演什么角色,而在后置条件中没有提到
node
可能会被更改(我猜你应该从它是指针的引用这一事实推断出来)。因为在需求中没有提到字符串,你可能会忽略它。或者,这只是为了让你回到问题的制造者那里,要求澄清。newNode->set_link(0);这里的0将与NULL相同?是的。我是一个老学校C++的人,所以我用<代码> 0代码>来表示空指针。(代码)> NUL/<代码>实际上是C的东西,在C++中它被劝阻了一段时间,因为它通常是<代码>定义< /代码>宏,因此它不能进入<>代码> STD< /Cord>命名空间,还有其他几个原因。在C++11及更高版本中执行此操作的正确方法是使用
NULL ptr
,因为它比
0
NULL
具有更好的类型安全性。非常感谢!我刚刚在code和我的编译器上遇到了一个小问题。节点*项=&head;我得到一个错误,说“无法将节点**转换为初始化中的节点*”使用节点*项=头;我以为我们不想那样做是因为(漏水)?如上所述。但如果我这样做,它会起作用的。