<分区>
您好,我写了一个代码来添加替代节点,该节点在单链表中包含整数值。我的代码崩溃了,请帮我修复它。
例子让单链表中有 6 个节点 3 5 8 6 4 9 那么 o/p 应该是 3+8+4 和 5+6+9,我想我的方法是错误的,请帮助我修复它。在下面的代码中,我只返回一个替代值,即 3+8+4?
void add(struct st **ptr)
{
struct st *curr,*prev;
curr=*ptr;
while(curr->next!=NULL)
{
if(curr->next->next->data!=NULL) //checking alternative node is present or
//or not and to avoid crash
{
sum= curr->data + curr->next->next->data;
}
else
{
sum= curr->data;
}
curr= curr->next;
}
prev=*ptr;
while(prev->next !=null)
{
prev=prev->next;
if(prev->next->next->data!=NULL)
{
sum=prev->data+prev->next->next->data;
}
else
{
sum=prev->data;
}
}
return sum;
}