编写一个函数来复制C ++中的链表 [英] Coding a function to copy a linked-list in C++
问题描述
我需要实现一个辅助函数,名为copyList,有一个参数,一个指向ListNode的指针。此函数需要返回一个指向原始链表副本的第一个节点的指针。因此,换句话说,我需要在C ++中编写一个函数,该函数接收链表的头节点并复制整个链表,返回一个指向新头节点的指针。我需要帮助实现这个函数,这是我现在现在。
I need to implement an auxilliary function, named copyList, having one parameter, a pointer to a ListNode. This function needs to return a pointer to the first node of a copy of original linked list. So, in other words, I need to code a function in C++ that takes a header node of a linked list and copies that entire linked list, returning a pointer to the new header node. I need help implementing this function and this is what I have right now.
Listnode *SortedList::copyList(Listnode *L) {
Listnode *current = L; //holds the current node
Listnode *copy = new Listnode;
copy->next = NULL;
//traverses the list
while (current != NULL) {
*(copy->student) = *(current->student);
*(copy->next) = *(current->next);
copy = copy->next;
current = current->next;
}
return copy;
}
此外,这是我正在使用的Listnode结构:
Also, this is the Listnode structure I am working with:
struct Listnode {
Student *student;
Listnode *next;
};
注意:使用这个函数的另一个因素是返回一个指针到本地变量。
Note: another factor I am running into with this function is the idea of returning a pointer to a local variable.
推荐答案
你需要问自己的第一个问题是什么是复制语义。特别是,你使用 Student *
作为节点内容。复制节点内容是什么意思?如果我们复制指针,使两个列表将指向(共享)相同的学生实例,或者您应该执行深度复制?
The first question you need to ask yourself is what the copy semantics are. In particular, you're using a Student*
as node contents. What does copying node contents mean? Should we copy the pointer so that the two lists will point to (share) the same student instances, or should you perform a deep copy?
struct Listnode {
Student *student; // a pointer? shouldn't this be a `Student` object?
Listnode *next;
};
下一个问题你应该问自己是如何为第二个列表分配节点。目前,您只在副本中分配1个节点。
The next question you should ask yourself is how you will allocate the nodes for the second list. Currently, you only allocate 1 node in the copy.
我认为您的代码应该看起来更像:
I think you code should look more like:
Listnode *SortedList::copyList(Listnode *L) {
Listnode *current = L;
// Assume the list contains at least 1 student.
Listnode *copy = new Listnode;
copy->student = new Student(*current->student);
copy->next = NULL;
// Keep track of first element of the copy.
Listnode *const head = copy;
// 1st element already copied.
current = current->next;
while (current != NULL) {
// Allocate the next node and advance `copy` to the element being copied.
copy = copy->next = new Listnode;
// Copy the node contents; don't share references to students.
copy->student = new Student(*current->student);
// No next element (yet).
copy->next = NULL;
// Advance 'current' to the next element
current = current->next;
}
// Return pointer to first (not last) element.
return head;
}
两个列表之间的实例,可以使用
If you prefer sharing student instances between the two lists, you can use
copy->student = current->student;
而不是
copy->student = new Student(*current->student);
这篇关于编写一个函数来复制C ++中的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!