c++:链表错误" Access violation reading location"



我正在尝试为我的链表创建一个函数,该函数将一个节点添加到程序的末尾。我在节点中使用指向的链接时出现错误,该链接指出

访问冲突读取位置0xccccccd8

它在我的添加函数中指向 while 循环的开头。我该如何解决这个问题?

我的添加、打印和显示列表功能:

void List::Add(char data, char* dataString)
{
Node* n5;
n5 = new Node;
n5->data = data;
n5->dataString = dataString;
while (nodes->linkf != NULL)
{
    nodes = nodes->linkf;
    
}
nodes->linkb = n5;
//n5->linkb = List::nodes;
//n5->linkf = NULL;
}
void List::showList(int dir)
{
if (dir==1){
  while (nodes !=NULL)
   {
        Print();
        nodes = nodes->linkf;
   }
   cout<<"n";
}
if (dir==0){
   while(nodes != NULL)
   {
    Print();
    nodes = nodes->linkb;
   }
   cout<<"n";
   }
}
void List::Print()
{
    cout<<" n";
    cout<<nodes->data;
    cout<<nodes->dataString;
    cout<<" n";
 }

节点和列表类:

class Node
{
public:
    Node(){
        char data[5];
        dataString=new(char[10]);
    }
    ~Node(){};
    Node *linkb;
    char data;
    char* dataString;
    Node *linkf;
};
class List{
public:
Node* nodes;
List(){}
void Add(char data, char*dataString);
void showList(int);
void Print();
string entry;
};

主要:

Node *n1, *n2, *n3, *n4 ;
List *l1;
int direction;
char choice;
char data;
char* dataString = "";
char dataBuffer[30];
List myList;
n1 = new(Node);
n2 = new(Node);
n3 = new(Node);
n4 = new(Node);
string entry= "";
l1 =new(List);
cout << "Please input the data: " << endl;
cin >> data;
cout << "Please input the data string: " << endl;
cin >> dataBuffer;
dataString = &dataBuffer[0];


n1->linkb = NULL;
n1->data='C';
n1->dataString="ats ";
n1->linkf = n2;
n2->linkb = n1;
n2->data='L';
n2->dataString="ike ";
n2->linkf = n3;
n3->linkb = n4;
n3->data='F';
n3->dataString="ish ";
n3->linkf = NULL;
n4->linkb = n2;
n4->data='D';
n4->dataString="ont ";
n4->linkf = NULL;
l1->nodes = n1;
myList.Add(data, dataString);

cout<<"nShow linked list Forward (F) or Backward (B): ";
cin>>choice;
if (choice == 'F')
    {
    direction=1;
    l1->nodes=n1;
    l1->showList(direction);
    }
    else if (choice=='B')
    {
    direction=0;
    l1->nodes=n3;
    l1->showList(direction);
    }
      else
      {
      cout<<"INVALID ENTRY !";
      exit(1);
      }
delete (n1);
delete (n2);
delete (n3);
delete (n4);

问题就在这里:

当你调用myList.Add(data, dataString);时,myList.nodes的内容是不确定的,因为没有人初始化过它。

void List::Add(char data, char* dataString)
{
  Node* n5;
  n5 = new Node;
  n5->data = data;
  n5->dataString = dataString;
  while (nodes->linkf != NULL)    // nodes is undetermined here and therefore
                                  // dereferencing it crashes the program
  {
    nodes = nodes->linkf;
  }
  nodes->linkb = n5;
  //n5->linkb = List::nodes;
  //n5->linkf = NULL;
}

如果在 30 个 seconbds 中使用(猜猜怎么着)调试器发现这一点。

但是代码中的其他地方很可能有更多的问题,整个代码看起来很可疑。

查看您的代码,您永远不会将指针设置为 null,这意味着它们只会在构建时获取内存中的任何垃圾值。在node类构造函数中,您应该初始化指向null的所有指针:

    Node()
        : linkb(nullptr), linkf(nullptr)
    {
        char data[5];
        dataString=new(char[10]);
    }

您应该对list类执行相同的操作

所以我刚刚解决了它,尽管仍然有很多错误和写得很糟糕的代码,它在技术上是有效的。当我在 main 中声明节点等于 n1 时,我使用了与 add 函数不同的 List 对象。

我之前的代码:

l1->nodes = n1;
myList.Add(data, dataString);

我现在的代码:

l1->nodes = n1;
l1->Add(data, dataString);

最新更新