帮助递归函数

发布于 2024-10-16 00:02:54 字数 1604 浏览 2 评论 0原文

我有以下代码来合并两个排序的链表:

struct node* merge(struct node* a, struct node* b)
{
        struct node dummy;     

        struct node* tail = &dummy; 

        dummy.next = NULL;
        while(1)
        {
                if(a == NULL)
                {
                        tail->next = b;
                        break;
                }
                else if (b == NULL)
                {
                        tail->next = a;
                        break;
                }
                if (a->data <= b->data)
                {
                        MoveNode(&(tail->next), &a);
                }
                else
                {
                        MoveNode(&(tail->next), &b);
                }
                tail = tail->next;
        }
        return(dummy.next);
} 

void MoveNode(struct node** destRef, struct node** sourceRef)
{
        struct node* newNode = *sourceRef;

        *sourceRef = newNode->next;

        newNode->next = *destRef;

        *destRef = newNode;
}

并且效果很好。我试图将其变成递归方法,这就是我得到的:

struct node* Merge(struct node* a, struct  node* b)
{
        struct node* result;

        if (a == NULL)
                return(b);
        else if (b==NULL)
                return(a);

        if (a->data <= b->data)
        {                
                result = Merge(a->next, b);
        }
        else
        {                
                result = Merge(a, b->next);
        }
        return(result);
}

但结果中缺少许多节点。怎么了?

I have the following code to merge two sorted linked lists:

struct node* merge(struct node* a, struct node* b)
{
        struct node dummy;     

        struct node* tail = &dummy; 

        dummy.next = NULL;
        while(1)
        {
                if(a == NULL)
                {
                        tail->next = b;
                        break;
                }
                else if (b == NULL)
                {
                        tail->next = a;
                        break;
                }
                if (a->data <= b->data)
                {
                        MoveNode(&(tail->next), &a);
                }
                else
                {
                        MoveNode(&(tail->next), &b);
                }
                tail = tail->next;
        }
        return(dummy.next);
} 

void MoveNode(struct node** destRef, struct node** sourceRef)
{
        struct node* newNode = *sourceRef;

        *sourceRef = newNode->next;

        newNode->next = *destRef;

        *destRef = newNode;
}

And it worked fine. I was trying to make it into a recursive method and this is what I got:

struct node* Merge(struct node* a, struct  node* b)
{
        struct node* result;

        if (a == NULL)
                return(b);
        else if (b==NULL)
                return(a);

        if (a->data <= b->data)
        {                
                result = Merge(a->next, b);
        }
        else
        {                
                result = Merge(a, b->next);
        }
        return(result);
}

But it has many of the nodes missing in the result. What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

楠木可依 2024-10-23 00:02:54

你的基本条件是正确的。但是你的递归条件有问题。

当您将 a 的数据与 b 的数据进行比较时,您并未将节点 a 或节点 b 复制到其中结果

尝试:

struct node* result; 

if (a == NULL)         
        return(b);                     
else if (b==NULL)                              
        return(a);                                             

if (a->data <= b->data)                                                
{          
        // make result point to node a.                                        
        result = a;      
        // recursively merge the remaining nodes in list a & entire list b
        // and append the resultant list to result.
        result->next = Merge(a->next, b);
}
else                                    
{                
        result = b;
        result->next = Merge(a, b->next);               
}
return(result);

Your base conditions are correct. But there is problem with your recursive condition.

When you compare a's data with b's data you are not copying node a or node b into result.

Try:

struct node* result; 

if (a == NULL)         
        return(b);                     
else if (b==NULL)                              
        return(a);                                             

if (a->data <= b->data)                                                
{          
        // make result point to node a.                                        
        result = a;      
        // recursively merge the remaining nodes in list a & entire list b
        // and append the resultant list to result.
        result->next = Merge(a->next, b);
}
else                                    
{                
        result = b;
        result->next = Merge(a, b->next);               
}
return(result);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文