Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.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++_Pointers_Linked List_Segmentation Fault - Fatal编程技术网

C++ 为什么可以';这些节点不能链接在一起吗?

C++ 为什么可以';这些节点不能链接在一起吗?,c++,pointers,linked-list,segmentation-fault,C++,Pointers,Linked List,Segmentation Fault,编辑:是否可以不使用新建?(不动态分配内存) 我认为这是错误的,但我不知道在哪里,如何,为什么。代码如下: struct Node { string fileName; Node *link; }; int size(Node *&flist) { int count = 0; Node *tempPtr = flist; while (tempPtr != 0) { count += 1; tempPtr->

编辑:是否可以不使用
新建
?(不动态分配内存)

我认为这是错误的,但我不知道在哪里,如何,为什么。代码如下:

struct Node {
    string fileName;
    Node *link;
};
int size(Node *&flist) {
    int count = 0;
    Node *tempPtr = flist;
    while (tempPtr != 0) {
        count += 1;
        tempPtr->link = (tempPtr->link)->link;
    }
    return count;
}
Node* push(Node *&flist, string name) {
    Node temp;
    Node *tempPtr = &temp;
    temp.fileName = name;
    temp.link = flist;
    cout << tempPtr->fileName << endl;
    cout << (tempPtr->link)->fileName << endl;
    return tempPtr;
}
int main( int argc, char *argv[] ) {
        Node aNode;
    Node *flist = &aNode;
    flist->fileName = "a";
    flist->link = NULL;
    push(flist, "b");
    int s = size(flist);
    cout << "size: " << s << endl;
}
谢谢。

嗯,你的size()函数有点过头了。你可以试试

int size(Node *flist) {
    int count = 0;

    Node *tempPtr = flist;
    while (tempPtr) {
        count += 1;
        tempPtr=tempPtr->link;
    }

    return count;
}
我已经从while语句中删除了一个无关的退出条件,该条件阻止计算只有一个元素的列表的长度

它在您的版本中返回0的原因是您的while语句:

while ((tempPtr != 0) &&(tempPtr ->link != 0)) {
        count += 1;
        tempPtr->link = (tempPtr->link)->link;
    }
从不执行,因为您的一个节点的.link值为null(0)。试试我上面提供的修改版本

哦,将来,你可能想把这些帖子标记为“家庭作业”。你会得到更好的回复。

size()
函数中,你正在修改循环中的列表。您不想修改
temptr->link
,而只是在迭代时更改
temptr
。更改
temptr
不会永久修改任何内容。您还应该避免在此处通过引用传递
flist
,因为不需要修改它。因此:

int size(Node *flist) {
    int count = 0;
    Node *tempPtr = flist;
    while (tempPtr != 0) {
        count += 1;
        tempPtr = tempPtr->link;
    }
    return count;
}
至于
push()
,最大的问题是您将新节点分配为局部变量,这意味着它将位于堆栈上,并且在函数返回时将被销毁。要创建更持久的节点,需要使用
new
操作符在堆上分配它。同样地,
flist
的“&”是不必要的:

Node* push(Node *flist, string name) {
    Node *tempPtr = new Node;
    tempPtr->fileName = name;
    tempPtr->link = flist;
    cout << tempPtr->fileName << endl;
    cout << tempPtr->link->fileName << endl;
    return tempPtr;
}
Node*push(Node*flist,字符串名){
Node*temptr=新节点;
temptr->fileName=名称;
temptr->link=flist;

cout fileName文件名您需要使用
new
。否则变量
temp
将在
push
函数的末尾被销毁。稍后,如果您尝试访问指针指向的内容,它将消失。

我认为您缺少
main
中的一两行代码。特别是,没有提到file“b”也没有使用push()函数。size函数不应该有签名
int size(const Node*flist);
因为它当然不应该修改列表中的任何内容。Node不应该也有一个构造函数……让我们不要压倒他。构造函数是一大罐蠕虫……!为什么最好避免通过引用传递flist?如果我需要修改它怎么办?那么Node*&flist是至关重要的?@derdji:参数应该根据需要传递by函数。一般来说,函数应该定义得很好,并且应该表现出预期的行为。返回堆栈大小的函数不应该修改堆栈;因此堆栈参数不应该通过引用传递给该函数。另一方面,push/pop等函数应该修改堆栈d将需要通过引用传递堆栈参数。
Node* push(Node *flist, string name) {
    Node *tempPtr = new Node;
    tempPtr->fileName = name;
    tempPtr->link = flist;
    cout << tempPtr->fileName << endl;
    cout << tempPtr->link->fileName << endl;
    return tempPtr;
}