双链 list.Segmentation 故障
Doubly linked list.Segmentation fault
我的任务是删除奇数块。
例如:
0-4-5-7-9-10 到 0-4-10。
为了实现这一点,我编写了这个函数:
void delete_odd_el(Tsnum *&Start)
{
Tsnum *wp, *pp, *del_el;
wp = Start;
while(wp != nullptr)
{
if(wp->num % 2 == 1)
{
while(wp!=nullptr)
{
if(wp->num % 2 == 0) break;
del_el = wp;
wp = wp->next;
wp->prev = del_el->prev;
delete del_el;
}
}
wp = wp->next;
}
}
但是当我 运行 它时,我遇到了分段错误。知道如何解决这个问题吗?
伙计们,如果这对你有帮助:
void delete_odd_el(Tsnum *&Start)
{
Tsnum *wp;
wp = Start;
while(wp!= nullptr)
{
if(wp->num % 2 ==1 && wp->next !=nullptr && wp->next->num % 2 == 1)
{
while(wp != nullptr && wp->num % 2 == 1)
{
if(wp->prev !=nullptr)
{
wp = wp->prev;
if(wp->next->next != nullptr)
{
wp->next = wp->next->next;
delete wp->next->prev;
wp->next->prev = wp;
}
else
{
delete wp->next;
wp->next = nullptr;
}
wp = wp->next;
}
else
{
if(wp->next != nullptr)
{
wp = wp->next;
delete wp->prev;
wp->prev = nullptr;
Start = wp;
}
else
{
delete wp;
wp = nullptr;
Start = wp;
}
}
}
}
else
wp = wp->next;
}
}
我的任务是删除奇数块。 例如: 0-4-5-7-9-10 到 0-4-10。 为了实现这一点,我编写了这个函数:
void delete_odd_el(Tsnum *&Start)
{
Tsnum *wp, *pp, *del_el;
wp = Start;
while(wp != nullptr)
{
if(wp->num % 2 == 1)
{
while(wp!=nullptr)
{
if(wp->num % 2 == 0) break;
del_el = wp;
wp = wp->next;
wp->prev = del_el->prev;
delete del_el;
}
}
wp = wp->next;
}
}
但是当我 运行 它时,我遇到了分段错误。知道如何解决这个问题吗?
伙计们,如果这对你有帮助:
void delete_odd_el(Tsnum *&Start)
{
Tsnum *wp;
wp = Start;
while(wp!= nullptr)
{
if(wp->num % 2 ==1 && wp->next !=nullptr && wp->next->num % 2 == 1)
{
while(wp != nullptr && wp->num % 2 == 1)
{
if(wp->prev !=nullptr)
{
wp = wp->prev;
if(wp->next->next != nullptr)
{
wp->next = wp->next->next;
delete wp->next->prev;
wp->next->prev = wp;
}
else
{
delete wp->next;
wp->next = nullptr;
}
wp = wp->next;
}
else
{
if(wp->next != nullptr)
{
wp = wp->next;
delete wp->prev;
wp->prev = nullptr;
Start = wp;
}
else
{
delete wp;
wp = nullptr;
Start = wp;
}
}
}
}
else
wp = wp->next;
}
}