当我在C++中编码时,我得到了一个SIGSEGV错误。但是,当我在两者之间添加cout语句以知道哪里出错时,我的代码正在执行并给出正确的输出。
有没有人能建议如何克服这类问题?
代码(如果需要):(如果存在于链表中,则删除循环的代码):
void removeLoop(Node* head)
{
Node *slow=head,*fast=head;
int u=0;
while(slow !=NULL and fast->next!=NULL and fast!=NULL){
slow=slow->next;
fast=fast->next->next;
if(slow==fast){
u=1;
break;
}
}
if(u==1){
int k=1;
while(fast->next!=slow){
fast=fast->next;
k++;
}
slow=head;
fast=head;
for(int i=0;i<k;i++){
fast=fast->next;
}
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
while(fast->next!=slow){
fast=fast->next;
}
//cout<<fast->next->data<<endl;
fast->next=NULL;
}
}发布于 2021-02-07 13:56:20
仔细看看这个:
while(slow !=NULL and fast->next!=NULL and fast!=NULL){现在想想如果slow != NULL and fast == NULL..。
https://stackoverflow.com/questions/66084854
复制相似问题