C++ 删除在链接列表中找到的所有特定键
我试图在一个函数中删除链接列表中的所有特定关键元素。 也就是说,如果链表有1 2 2 3 4 4 5 5 8 2 6 32 4 6 7 7,那么如果我传递函数2,该函数将删除链表中的所有2 我的链接列表在这里C++ 删除在链接列表中找到的所有特定键,c++,pointers,coding-style,linked-list,C++,Pointers,Coding Style,Linked List,我试图在一个函数中删除链接列表中的所有特定关键元素。 也就是说,如果链表有1 2 2 3 4 4 5 5 8 2 6 32 4 6 7 7,那么如果我传递函数2,该函数将删除链表中的所有2 我的链接列表在这里 class float_list { struct node { double data; struct node *next; }; node *head; public: float_list(voi
class float_list
{
struct node
{
double data;
struct node *next;
};
node *head;
public:
float_list(void)
{
head = nullptr;
};
void appendNode(double);
void print_list();
void deleteNode(double);
};
现在我的deleteNode(这里是double)
void float_list::deleteNode(双数值)
{
节点*nextptr,*previousptr=nullptr;
nextptr=头部;
如果(!head->data){return;}
如果(头->数据==num)
{
NEXTPTTR=头部->下一步;
删除标题;
头=下一个TPTR;
}
其他的
while(nextptr)
{
前一个PTR=下一个PTTR;
如果(nextptr->data==num)
{
上一个ptr->next=nextptr->next;
删除nextptr;
我可以追踪到两个问题:
千万不要使用运算符==
来检查两个浮点数的相等性
点数字,浮点运算有问题-
它们并不精确,结果可能并不像你们期望的那样[不是你们问题的解决方案,而是一个明确的问题]
您的previousptr
和nextptr
是同一件事[它们都指向同一个地址!]。您应该在当前迭代之前修改previousptr
。[就在nextptr=nextptr->next;
]之前。因此,当您删除nextptr
并在以后设置:
nextptr = previousptr;
nextptr = nextptr->next;
实际上,您正在访问刚删除的元素,这会导致非法访问。while循环结束后会发生什么。嗯,nextptr==NULL。delete NULL==问题
试试这个:
node *previous = nullptr, *current = head, *temp;
while(current){
temp = current->next;
if(abs(current->data - num) < MARGIN_OF_ERROR){
if (previous){
previous->next = current->next;
} else {
head = current->next;
}
delete current;
} else{
previous = current;
}
current = temp;
}
节点*previous=nullptr,*current=head,*temp;
while(当前){
温度=当前->下一步;
if(abs(当前->数据-num)下一个=当前->下一个;
}否则{
头部=当前->下一步;
}
删除当前文件;
}否则{
先前=当前;
}
电流=温度;
}
类似的内容(伪代码)
这可能可以通过递归实现,但这里有一个移动指针解决方案:
void removenumber(node **head, int value)
{
node * current;
node * prev;
node * tmp;
current = (*head);
if ( current == NULL) return;
prev = current;
current = current->next;
while( current!= NULL && current->next != NULL)
{
if ( current->data == value)
{ //remove current
tmp = current->next;
prev->next = current->next;
free(current);
current = tmp;
//previous is the same
continue;
}
current = current->next;
prev = prev->next;
}
// now current->next != NULL check it
if(current!=NULL) //what is current was deleted?
{
if(current->data == value )
{
prev->next = NULL;
free(current);
}
}
if ( (*head)->data == value) //remove head
{
tmp = (*head)->next;
free(*head);
*head = tmp;
}
}
我还尝试了(nextptr->next!=nullptr),希望在O(n)中完成千万不要使用运算符==
来检查两个浮点数的相等性,浮点数运算有一个问题-它们不精确,结果可能不符合您的预期。您可以给出一个迭代解决方案吗?是的,这可以转化为一个迭代解决方案,但为什么不在需要的地方使用递归呢used?!@sparkling\u spark:逐步通过调试器并检查行delete nextptr;
。您将看到previousptr==nextptr
。因此-当您稍后设置nextptr=previousptr;
,并尝试访问nextptr
,您实际上正在访问已删除的内容,这将导致访问violat我保留的错误范围为0.0001,并尝试删除3.1和3.0,但从未删除该代码有一些错误,我已修复。请重试并通知我。
public void removeData( double data )
{
if ( this.node == null ){ return; }
if ( this.node->data == data ){
this.node = this.node.node;
}
this.node.removeData( data );
}
void removenumber(node **head, int value)
{
node * current;
node * prev;
node * tmp;
current = (*head);
if ( current == NULL) return;
prev = current;
current = current->next;
while( current!= NULL && current->next != NULL)
{
if ( current->data == value)
{ //remove current
tmp = current->next;
prev->next = current->next;
free(current);
current = tmp;
//previous is the same
continue;
}
current = current->next;
prev = prev->next;
}
// now current->next != NULL check it
if(current!=NULL) //what is current was deleted?
{
if(current->data == value )
{
prev->next = NULL;
free(current);
}
}
if ( (*head)->data == value) //remove head
{
tmp = (*head)->next;
free(*head);
*head = tmp;
}
}