C++ C++;链表列表\复制\前台功能混乱

C++ C++;链表列表\复制\前台功能混乱,c++,linked-list,copy,nodes,C++,Linked List,Copy,Nodes,我一直在努力使用最后一个函数(list\u copy\u front)。该函数用于链表,它应该返回包含源指针指向的前n个节点副本的新列表的头指针的值。此外,如果源中的节点少于n个,则只需复制所有节点。目前,当我按原样运行它时,我得到了一个严重的分段错误SIGSEGV错误。调试器说错误发生在“Node*cursor=source\u ptr->link”;如果您有任何帮助,我们将不胜感激,谢谢 以下是一些相关信息 struct Node { typedef int Item; It

我一直在努力使用最后一个函数(list\u copy\u front)。该函数用于链表,它应该返回包含源指针指向的前n个节点副本的新列表的头指针的值。此外,如果源中的节点少于n个,则只需复制所有节点。目前,当我按原样运行它时,我得到了一个严重的分段错误SIGSEGV错误。调试器说错误发生在“Node*cursor=source\u ptr->link”;如果您有任何帮助,我们将不胜感激,谢谢

以下是一些相关信息

struct Node
{
    typedef int Item;
    Item data;
    Node *link;
};

void list_tail_attach(Node*& head_ptr, const Node::Item& entry);
Node* list_copy_front(Node* source_ptr, size_t n);

void list_tail_attach(Node*& head_ptr, const Node::Item& entry)
{
    Node *last = new Node;

    last->data = entry;
    last->link = NULL;

    if(head_ptr == NULL)
    {
        head_ptr = last;
    }
    else
    {
        Node *temp = new Node;
        temp = head_ptr;
        while(temp->link != NULL)
        {
            temp = temp->link;
        }
        temp->link = last;
    }
}

Node* list_copy_front(Node* source_ptr, size_t n)
{
    Node *new_head_ptr = new Node;
    Node *cursor = source_ptr->link;
    size_t i = 0;

    for(i = 0; i < n; i++)
    {
        list_tail_attach(new_head_ptr, cursor->data);
        cursor = cursor->link;
    }
    return new_head_ptr;
}
struct节点
{
typedef int项;
项目数据;
节点*链接;
};
无效列表\尾部\附加(节点*和头部\ ptr,常量节点::项目和条目);
节点*列表\副本\前端(节点*源\ ptr,大小\ n);
无效列表\尾部\附加(节点*和头部\ ptr,常量节点::项目和条目)
{
Node*last=新节点;
最后->数据=输入;
last->link=NULL;
if(head_ptr==NULL)
{
头部=最后一个;
}
其他的
{
Node*temp=新节点;
温度=压头温度;
while(临时->链接!=NULL)
{
温度=温度->链接;
}
临时->链接=最后一个;
}
}
节点*列表\副本\前端(节点*源\ ptr,大小\ n)
{
节点*new_head_ptr=新节点;
节点*cursor=source\u ptr->link;
尺寸i=0;
对于(i=0;i数据);
光标=光标->链接;
}
返回新的\u头\u ptr;
}
下面是该函数的主要测试

int test4()
{
    Node* list = NULL; // an empty list
    Node* copy = NULL;
    copy = list_copy_front(list, 3);
    if(copy != NULL)
    {
        cout << "list_copy_front function doesn't work for copying empty list\n";
        return 0;
    }
    for(int i = 1; i <= 4; i++)
        list_tail_attach(list, i);
    // list contains 1, 2, 3, 4

    copy = list_copy_front(list, 3);
    if(list_length(copy) != 3 || copy->data != 1 || copy->link->data != 2 || copy->link->link->data != 3 )
    {
        cout << "list_copy_front function doesn't work\n";
        return 0;
    }

    copy->link->data = 100;
    if(list->link->data == 100)
    {
        cout << "list_copy_front function doesn't work.\n";
        return 0;
    }
    list_clear(copy);
    copy = list_copy_front(list, 6);
    if(list_length(copy) != 4)
    {
        cout << "list_copy_front function doesn't work\n";
        return 0;
    }

    cout << "list_copy_front passes the test\n";

    list_clear(list);
    for(int i = 1; i <= 3; i++)
        list_head_insert(list, i);
    // list contains 3, 2, 1

    list_copy(list, copy);
    if(list_length(copy) != 3 || copy->data != 3 || copy->link->data != 2 || copy->link->link->data != 1 )
    {
        cout << "list_copy function doesn't work\n";
        return 0;
    }

    cout << "list_copy function passes the test\n";

    return 2;

}
inttest4()
{
Node*list=NULL;//一个空列表
Node*copy=NULL;
副本=列表\副本\前端(列表,3);
如果(复制!=NULL)
{
无法链接->数据!=2(复制->链接->链接->数据!=3)
{
cout link->data=100;
如果(列表->链接->数据==100)
{
cout link->link->data!=1)
{
cout第一个测试用例

Node* list = NULL; // an empty list
Node* copy = NULL;
copy = list_copy_front(list, 3);
给出Node*source_ptr==NULL,并希望您的函数能够优雅地处理它。 函数代码很快尝试取消对NULL的引用

Node *cursor = source_ptr->link;

结果是一个segfault。

首先是
list\u tail\u attach
,这个函数我假设是将一个现有的
节点
附加到一个链表中。如果链表为
null
,那么
节点
将成为
头部

void list_tail_attach(Node *& head_ptr, Node *& entry)
{
    if (entry == NULL) {
        return;
    }
    if (head_ptr == NULL)
    {
        head_ptr = entry;
    }
    else
    {
        Node *temp = head_ptr;
        while (temp->link != NULL)
        {
            temp = temp->link;
        }
        temp->link = entry;
    }
}
我将
条目
更改为指向的指针的引用,以使其更简单

好的,现在转到
列表\u copy\u front

Node * list_copy_front(Node* source_ptr, size_t n)
{
    if (source_ptr == NULL) {
        return NULL;
    }
    Node * new_head_ptr = new Node;
    Node * cursor = source_ptr;

    size_t i = 0;

    while(cursor != NULL && i < n){
        list_tail_attach(new_head_ptr, cursor);
        cursor = cursor->link;
        i++;
    }

    return new_head_ptr;
}

我的教授帮我找到了正确的解决方案。对于任何未来对此有看法的人

Node* list_copy_front(Node* source_ptr, size_t n)
{
    if(source_ptr == NULL)  // Takes care of NULL case
    {
       return NULL;
    }
    Node *new_head_ptr = NULL;  // Creates new head and ensures NULL
    Node *cursor = source_ptr;  // Sets temp Node = to source

    size_t i = 0;   // Initializes temp variable

    while(cursor!= NULL && i < n)   // Loop that continues while n is bigger than i and it is not NULL
    {
        list_tail_attach(new_head_ptr, cursor->data);
        cursor = cursor->link;  // Attaches to new list
        i++;    // Increases count
    }
    return new_head_ptr;
}
节点*列表\副本\前端(节点*源ptr,大小\u t n)
{
if(source_ptr==NULL)//处理NULL大小写
{
返回NULL;
}
Node*new\u head\u ptr=NULL;//创建新head并确保为NULL
Node*cursor=source\u ptr;//将temp Node=设置为source
size\u t i=0;//初始化临时变量
while(cursor!=NULL&&i数据);
游标=游标->链接;//附加到新列表
i++;//增加计数
}
返回新的\u头\u ptr;
}
需要更改的行是
节点*new_head_ptr=新节点; 到
Node*new\u head\u ptr=NULL;

你能附加
main
程序吗,似乎
source\u ptr
不是
NULL
,而是
source\u ptr->link
NULL
我在前面添加了这个
copy=list\u copy\u(列表,3);
会导致错误吗?我不相信,你能解释一下你为什么这么认为吗?@AchmadJP你的评论有相同的信息,比我的答案快。为什么不把它作为一个答案呢?它可以被接受为更快的答案。所以要在不改变测试函数的情况下解决这个问题。我需要调整list_copy_函数来处理空情况?这是我的建议理解你的问题,是的。谢谢你提供的信息,我不知道这个for循环会放在哪里。我也在我的帖子中添加了信息。
for (int x = 0; x < 5; x++) {
    Node * tmp = new Node();
    tmp->data = x;
    tmp->link = NULL;
    list_tail_attach(list, tmp);
}
Node* list_copy_front(Node* source_ptr, size_t n)
{
    if(source_ptr == NULL)  // Takes care of NULL case
    {
       return NULL;
    }
    Node *new_head_ptr = NULL;  // Creates new head and ensures NULL
    Node *cursor = source_ptr;  // Sets temp Node = to source

    size_t i = 0;   // Initializes temp variable

    while(cursor!= NULL && i < n)   // Loop that continues while n is bigger than i and it is not NULL
    {
        list_tail_attach(new_head_ptr, cursor->data);
        cursor = cursor->link;  // Attaches to new list
        i++;    // Increases count
    }
    return new_head_ptr;
}