将链接列表复制到另一个列表?

时间:2015-04-16 14:27:05

标签: c++ pointers linked-list copy duplicates

我需要链接列表中的帮助,以创建将列表复制到另一个列表的功能

我使用Visual Studio 2012进行编码。这是我的代码和错误:

错误:

  

链接list.exe中0x0111544F处的未处理异常:0xC0000005:   访问冲突写入位置0xCDCDCDCD。

有人可以告诉我错误是什么以及如何修复它?

#include <iostream>
using namespace std;

struct node
{
    int info;
    node *next;
};

void PrintList (node *C)
{  
    node *P;
    cout<<"Node content = (";
    P = C;
    while(P!=NULL)
    {
        cout<<P->info;
        P = P->next;
        if(P != NULL) 
            cout<<",";       
    }
    cout<<")"<<endl;
}

void copylist (node *Y, node **Z)
{
    node *P, *Q;
    int temp;
    if (Y==NULL)
    {
        cout<<"the list is empty"<<endl;
        *Z=NULL;
    }
    else
    {
        P=Y;
        Q=*Z;
        while (P->next!=NULL)
        {
            temp=P->info;
            Q->info=temp;
            P=P->next;
            Q=Q->next;
        }
    }
}

void insertnode (int *A, node **Q)
{
    node *N, *P;

    N=new node;
    N->info=*A;
    N->next=NULL;

    if(*Q==NULL)
        *Q=N;
    else
    {
        P=*Q;

        while (P->next!=NULL)
            P=P->next;

        P->next=N;
    }
}

int main ()
{
    node *z;
    node *duplicate;
    z=NULL;
    duplicate=new node;
    int e;
    int i;
    int *temp;
    temp=new int;

    cout<<"the number of element : ";
    cin>>e;

    cout<<" LIST Z CONTENT : " <<endl;
    PrintList (z);

    for (i=0;i<e;i++)
    {
        cin>>*temp;
        insertnode(temp, &z);
    }

    copylist(z,&duplicate);

    cout<<" DUPLICATE LIST CONTENT : "<<endl;
    PrintList(duplicate);
}

2 个答案:

答案 0 :(得分:0)

请注意,在main函数中,您尝试使用以下代码打印空列表:

cout<<" LIST Z CONTENT : " <<endl;
PrintList (z); 

因此,您可能希望将打印功能修改为:

void PrintList(node *C)
{
    if (C == NULL) return; // return if the list is empty or print out a message
    node *P = C;
    cout << "Node content = (";

    while (P != NULL)
    {
        cout << P->info;
        P = P->next;
        if (P != NULL)
            cout << ",";
    }
    cout << ")" << endl;
}

在您的复制功能中,您忘记创建新节点并在复制过程中保持与其的链接。以下是可行的代码:

void copylist(node *Y, node **Z)
{
    if (Y == NULL)
    {
        cout << "the list is empty" << endl;
        *Z = NULL;
    }
    else
    {
        node*P = Y;
        node *Q = NULL;

        while (P != NULL)
        {
            node* newNode = new node; // first create a node (allocate memory)
            newNode->info = P->info; // then fill the info
            newNode->next = NULL;
            if (Q == NULL)
            {
                Q = newNode;
                *Z = Q;
            }
            else
            {
                Q->next = newNode;
                Q = Q->next;
            }
            P = P->next;
        }
    }
}

最后,你的主要功能不会返回任何东西!您可能想要添加

return 0;

最后。

尝试用以下方法替换主要功能:

int main()
{
    node *z = NULL;
    node *duplicate = NULL;

    int e;
    cout << "The number of elements : ";
    cin >> e;

    cout << "enter elements with a space between them : ";
    while (e--)
    {
        int temp;
        cin >> temp;
        insertnode(temp, &z);
    }

    cout << " LIST Z CONTENT : " << endl;
    PrintList(z);

    copylist(z, &duplicate);

    cout << " DUPLICATE LIST CONTENT : " << endl;
    PrintList(duplicate);

    cin >> e;

    return 0;
}

或者,要将列表复制到另一个列表,可以在循环中调用insertnode函数,以便将原始列表的所有数据添加到循环中的新列表中。这将使您的复印功能更短。

有关链接列表的更多信息:

http://en.wikipedia.org/wiki/Linked_list

快乐的编码!

答案 1 :(得分:0)

如果不分配内存,则无法复制列表。

您有访问冲突导致您在复制时未在目标列表中创建节点,但在使用时{i} next依赖于任何(损坏的指针) node。 除此之外,你的内存泄漏很少。

注意带注释的行:

void copylist (node *Y, node **Z)
{
    node *P, *Q;
    if (Y==NULL)
    {
        cout<<"the list is empty"<<endl;
        //*Z=NULL;        // memory leak! lost pointer of caller!
    }
    else
    {
        P=Y;
        Q=*Z;
        while (P->next!=NULL)
        {
            Q->info = P->info;
            P=P->next;
            Q->next = new node();      // missing allocation!
            Q = Q->next;
        }
        Q->next = null;                // close new list.
    }
}