简体   繁体   English

递归复制链接列表

[英]Recursively Copying Linked List

After Googling around for a while I've come to the conclusion that I'm stumped. 谷歌搜索了一段时间后,我得出了自己很沮丧的结论。

The Problem: 问题:

Recursively copy a linked list (as well as copy in reverse, but I'll cross that bridge later.) 递归地复制一个链表(以及反向复制,但我稍后会越过该桥)。

I have the following code: 我有以下代码:

// In list.h
struct node {
    int data;
    node * next;
}

// In list.cpp
// Recursive copy wrapper
int list::copy() {                              
    if(!head) {                                 
        return 0;                               
    }                                           
    node *new_node = NULL;
    return copy(new_node, head);                                          
}                                               

// Recursive copy function
int list::copy(node *& dest, node * src) {      
    if(!src) {                                  
        dest = NULL;                            
        return 0;                               
    }                                           

    dest = new node();                          
    dest->data = src->data;
    // *dest->next = src->next;* // Not sure what to do here?

    return copy(dest->next, src->next) + 1; // count number of nodes copied  
}

Note : this is not a homework assignment but rather a question for a preparatory technical interview exam. 注意 :这不是家庭作业,而是准备技术面试的问题。

At this point, I'm fairly certain I won't be able to achieve this on my own so any help with it would be appreciated. 在这一点上,我相当确定我将无法独自实现这一目标,因此,我们将不胜感激。 Thanks in advance! 提前致谢!

To my understanding, the list needs to be recursively copied first and the reference of the new head needs to point to the head of the copy; 据我了解,列表需要首先递归地复制,并且新标题的引用需要指向副本的标题; this can be done as follows. 这可以按如下进行。

int list::copy(node *& dest, node * src)
{      
    if(!src)
    {                                  
        dest = NULL;                            
        return 0;                               
    }                                           

    dest = new node();                          
    dest->data = src->data;

    node* TailCopy = null; // reference to copy of remaining list

    int TotalNumOfNodes = 1 + copy(Tail, src->next) + 1;

    dest->next = TailCopy; // let copy of head refer to copy of tail

    return TotalNumOfNodes;
}

Well, the int list::copy(node *& dest, node * src) is perfectly correct and successfully copies a list tail up to another list tail. 好吧, int list::copy(node *& dest, node * src)完全正确,并且可以成功地将列表尾部复制到另一个列表尾部。 The problem lies in the int list() one which is plain non sense: you successfully copies all the nodes of current list in a new chain, and irremediably leak all that memory when done! 问题出在int list()这个问题上,这显然是没有意义的:您成功地将当前列表的所有节点复制到了新链中,并且在完成时不可避免地泄漏了所有内存!

If you want to build something that makes sense, you could use your recursive copy in a copy constructor: 如果要构建有意义的内容,可以在副本构造函数中使用递归副本:

list(const list& other) {
    copy(head, other.head);
}

This does not use the return value of the copy (could be static) method, by I can confirm that it is the expected value. 这不使用copy (可以是静态)方法的返回值,通过我可以确认它是期望值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM