Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.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_Pointers_Linked List_Singly Linked List - Fatal编程技术网

C++ 链表,我的逻辑哪里有缺陷?

C++ 链表,我的逻辑哪里有缺陷?,c++,list,pointers,linked-list,singly-linked-list,C++,List,Pointers,Linked List,Singly Linked List,当然,我已经查阅了很多链表帮助,但我似乎无法找出我的链表出了什么问题。我想我理解其他代码的逻辑,但我的代码出了问题,我无法让它正常工作 功能代码: void SparseM_list::newTerm(valueType newValue, int row, int column) MatrixTerm *n = new MatrixTerm; n->next = NULL; n->column = column; n->row

当然,我已经查阅了很多链表帮助,但我似乎无法找出我的链表出了什么问题。我想我理解其他代码的逻辑,但我的代码出了问题,我无法让它正常工作

功能代码:

void SparseM_list::newTerm(valueType newValue, int row, int column)
MatrixTerm *n = new MatrixTerm;
        n->next = NULL;
        n->column = column;
        n->row = row;
        n->value = newValue;
        if (head != NULL)
        {
            cur = head;    
            while (cur->next != NULL) 
            {
                cur = cur->next;
                cout << "does it ever get inside the while loop? cur and curnext -> " << cur << " " << cur->next << endl; <-- never outputs
            }
            cur->next = n;  
        }
        else  //if head is null, n will be the starting point
        {
            head = n;
        }
        delete n;

    }
所以基本上我的逻辑是

  • 新的术语信息动态分配给矩阵术语n
  • 如果head为null(由默认构造函数设置),则head=n
  • 第二组数据进入。头!=Null,因此我将cur指针设置为等于head
  • 对于第二个数据跳过while循环,因为head->next应该为null,所以cur->next应该为null。我将cur->next设置为n
  • 第三个数据进入。Cur->next与上一个有n个,因此它进入while循环。当前设置为cur->next。它检查while循环条件,这次cur->next应该为null,因此它转到设置cur->next=n(第三个数据集)
  • 但是,它从未进入while循环。我在哪里搞砸了?while循环用于遍历链接列表

    这句话

    delete n;
    
    没有道理。移除它

    我希望最初数据成员
    head
    确实设置为
    NULL
    (或
    nullptr

    该函数的另一个实现如下所示

    void SparseM_list::newTerm(valueType newValue, int row, int column)
    {
        MatrixTerm *n = new MatrixTerm { newValue, column, row, nullptr };
    
        MatrixTerm **current = &head;
    
        while ( *current ) current = &( *current )->next;
    
        *current = n;
    }
    
    如果列表允许附加新节点,那么还可以声明一个数据成员
    tail
    。在这种情况下,将向尾部添加一个新节点,该节点比每次执行循环时更有效


    还应考虑删除数据成员
    cur
    prev
    ,并将它们用作方法的局部变量。

    您不应
    删除n,因为它将释放列表节点的内存。你看,你一直把钥匙插进锁里,但在开门之前你把钥匙拔出来……你能进房子吗


    ps,删除节点应该保存在列表对象的Destructor中。

    您实际上是在分配指针,然后删除它,因此每次cur->next都将指向NULL
    void SparseM_list::newTerm(valueType newValue, int row, int column)
    {
        MatrixTerm *n = new MatrixTerm { newValue, column, row, nullptr };
    
        MatrixTerm **current = &head;
    
        while ( *current ) current = &( *current )->next;
    
        *current = n;
    }