链表分割错误c ++

时间:2020-05-25 17:12:27

标签: c++ linked-list

因此,我在创建具有多个链表的程序时,在链表上创建新节点时会出现分段错误。

这是一个有关如何即时定义列表以及如何创建节点的示例

#include <iostream>
#include <string>

using namespace std;

class node_A
{
public:
    string nameA;
    unsigned int codeA;

    node_A* nextA;
};


class list_A
{
private:
    node_A* headA;

public:
    list_A()
    {
        headA=NULL;
    }
    ~list_A()
    {
            node_A *current, *nextA;
            current=headA;
            while(current!=NULL)
            {
                nextA=current->nextA;
                delete current;
                current=nextA;
            }
    }
    unsigned int lastNodesCode()
    {
        node_A *current=headA;
       if(headA==NULL)
       {
           return 0;
       }
       else
        {
            while(current!=NULL)
            {
                if(current->nextA==NULL)
                {
                    return current->codeA;
                }
                else
                {
                    current = current->nextA;
                }
            }
        }
    }
    void newNodeA()
    {
        node_A *newNode=new node_A;
        node_A *current;
        string nameA;

        newNode->codeA=lastNodesCode()+1;

        cout<<"Name A: ";
        getline(cin,nameA);
        newNode->nameA = nameA;



        newNode->nextA=NULL;
        if(headA==NULL)
        {
        headA=newNode;
        }
        else
        {
            current=headA;
            while(current->nextA!=NULL)
                current=current->nextA;
            current->nextA=newNode;
        }
    }


};

class node_B
{
public:
    string nameB;
    unsigned int codeB;

    node_B* nextB;
};


class list_B
{
private:
    node_B* headB;

public:
    list_B()
    {
        headB=NULL;
    }
    ~list_B()
    {
            node_B *current, *nextB;
            current=headB;
            while(current!=NULL)
            {
                nextB=current->nextB;
                delete current;
                current=nextB;
            }
    }
    unsigned int lastNodesCodeB()
    {
        node_B *current=headB;
       if(headB==NULL)
       {
           return 0;
       }
       else
        {
            while(current!=NULL)
            {
                if(current->nextB==NULL)
                {
                    return current->codeB;
                }
                else
                {
                    current = current->nextB;
                }
            }
        }
    }
    void newNodeB()
    {
        node_B *newNode=new node_B;
        node_B *current;
        string nameB;

        newNode->codeB=lastNodesCodeB()+1;

        cout<<"Name B: ";
        getline(cin,nameB);
        newNode->nameB = nameB;



        newNode->nextB=NULL;
        if(headB==NULL)
        {
        headB=newNode;
        }
        else
        {
            current=headB;
            while(current->nextB!=NULL)
                current=current->nextB;
            current->nextB=newNode;
        }
    }


};


int main()
{
    list_A A;
    list_B B;

    A.newNodeA();
    A.newNodeA();


    B.newNodeB();
    B.newNodeB();
}

在我的代码中,当我尝试在链表中添加新节点而不是第一个时,它出现了分段错误。 但是在这个例子中,它没有这个问题,我不能在这里发布整个代码,因为它很大,我有8个链表

0 个答案:

没有答案