C ++ Linked List list_copy_front函数混淆

时间:2017-03-21 00:20:11

标签: c++ linked-list copy nodes

我一直在努力解决这个最后一个功能(list_copy_front)。该函数用于链表,它应该返回一个新列表的头指针值,该列表包含源指针指向的前n个节点的副本。此外,如果源中的节点少于n个,则只复制所有节点。目前,当我运行它时,我得到一个令人讨厌的Segmentation Fault SIGSEGV错误。调试器说错误发生在" Node * cursor = source_ptr->链接;非常感谢任何帮助,谢谢。

以下是一些相关信息,

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;
}

这是功能的主要测试

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;

}

编辑3 到目前为止,我正在与我合作,我很欣赏这些评论到目前为止它还没有完成。不好解释可能是我的错。

void list_tail_attach(Node*& head_ptr, const Node::Item& entry)
{
    Node *last = new Node;  // Creates new Node
    last->data = entry; // Points last to data
    last->link = NULL;

    if(last == NULL)
    {
        return;
    }

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


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->data);
        cursor = cursor->link;
        i++;
    }

    return new_head_ptr;
}

我不允许更改函数的输入方式,这就是我最后离开Node *的原因。 我离开了list_tail_attach(new_head_ptr,cursor-&gt; data)因为没有它我得到了无效的转换错误。但是,当我运行上面的代码时,我仍然在list_tail_attach和list_tail_attach(new_head_ptr,cursor-&gt; data)上收到while(temp-&gt; link!= NULL)的SIGSEGV错误;在list_copy_front中。

如果你能够进一步评论,谢谢你

3 个答案:

答案 0 :(得分:0)

第一个测试用例

"\\s+"

给出Node * source_ptr == NULL并期望你的函数优雅地处理它。 函数代码很快就会尝试取消引用NULL

Node* list = NULL; // an empty list
Node* copy = NULL;
copy = list_copy_front(list, 3);

结果是段错误。

答案 1 :(得分:0)

首先是 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="8dp" android:paddingBottom="8dp" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="UserID" android:textAppearance="?android:attr/textAppearanceLarge" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:text="User name" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="16sp" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_marginLeft="10dp" android:background="#FFFFFF" android:focusable="false" android:focusableInTouchMode="false" android:text="Add Friend" android:textColor="#0099CC" /> </LinearLayout> ,我假设这个函数将现有的list_tail_attach附加到链表中。如果链接列表为Node,则null将成为Node

head

我将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; } } 更改为对指针的引用,使其更容易。

好的,现在转到entry

list_copy_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; } ,您必须保护source_ptr

附加新的null

Node

答案 2 :(得分:0)

我的教授用正确的解决方案帮助了我。对于任何在将来看到这一点的人......

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;
}

需要更改的行是
Node * new_head_ptr = new Node; 至 Node * new_head_ptr = NULL;