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

删除双链表的最后一个节点(c++)

删除双链表的最后一个节点(c++),c++,doubly-linked-list,C++,Doubly Linked List,我一直在为学校做一个涉及双链接列表的项目,我在删除列表的最后一个节点时遇到了麻烦 bool DLL::remove(string ss, int & count){ Node* temp = new Node; if(headPtr == NULL){ return false;

我一直在为学校做一个涉及双链接列表的项目,我在删除列表的最后一个节点时遇到了麻烦

bool DLL::remove(string ss, int & count){

    Node* temp = new Node;                              

    if(headPtr == NULL){                                
        return false;                                   
    }
    else{                                               
        temp = headPtr;                                 
        for(int i = 0; i < itemCount-1; i++){           

            if(temp->ssn.compare(ss) == 0){             

                if(temp->pred == NULL){                 
                    count += 1;                         
                    itemCount -= 1;                     
                    headPtr = temp->succ;               
                    return true;
                }
                if(temp->succ != NULL){                 
                    temp->succ->pred = temp->pred;      
                    temp->pred->succ = temp->succ;      
                    return true;
        }
        if(temp->succ == NULL){     
            temp->pred->succ = NULL;
            return true;
        }

    }
    else{
        if(temp->succ == NULL){                 
            return false;                       
        }
        temp = temp->succ;  
    }
        }
    }
    return false;
}
< >我的代码工作在项目,或者是,我现在想的那个节点,是在中间的开始或任何地方,但是当它是列表中的最后一个项目时,它不删除它。 样本输出:

名单:10、20、30、40、50 当我去掉10,30和50,结果是 名单:20、40、50

任何帮助都将不胜感激。 根据我的教授的说法,succ代表继任者,pred代表前任。

您迭代到itemcount-2而不是itemcount-1,因此忽略最后一个元素,因为您使用的是
for (int i = 0; i < itemCount; i++) {
} 

除了您的问题之外,没有必要使用new分配temp,因为它通过分配给head等来获得一个值。您还应该在删除的节点上调用delete,否则会泄漏内存。

请修复缩进。至于您的问题,如果您以前没有这样做,现在是学习如何使用调试器的好时机。使用调试器,您可以逐行查看代码,查看发生了什么,并监视变量及其值以及对其值的更改。为什么要在名为remove的函数中分配一个新节点?Show an。非常感谢这一点帮助很大,至于delete,我知道我也忘了一些东西
Node *node;
for (node = head; node != NULL; node = node->succ) {
}