链接表中读取位置0xCCCCCCCC错误

Access violation reading location 0xCCCCCCCC error in linklist

本文关键字:0xCCCCCCCC 错误 位置 读取 链接表      更新时间:2023-10-16

为什么在调用showNode()时会出现上述错误?

#include<iostream>
#define NULL 0
using namespace std;
class myNode{
private:
    int data;
    myNode* link;
    myNode* first;
public:
    myNode(){
        data=0;
        link=NULL;
        first=NULL;
    }
    void insertNode(int value, int iposition){
        myNode n;
        if (iposition==1)
        {
            first=&n;
            cout<<first<<endl;
            n.data=value;
            n.link=NULL;
        }
        if (iposition>1)
        {
            int nodeCounter=1;
            myNode* temp=first;
            while (temp->link != NULL)
            {
                nodeCounter++;
            }// this while counts the number of inserted nodes.
            if (iposition>nodeCounter)//if the position of the new node is greater than number of inserted node,
                                        //it will be inserted at the end.
            {
                cout<<"Node will be inserted at end."<<endl;
                myNode* ieTemp=first;
                while (ieTemp->link != NULL)
                {
                    ieTemp=ieTemp->link;
                    cout<<ieTemp->data<<endl;
                }
                ieTemp->link=&n;
                n.data=value;
                n.link=NULL;
                cout<<&n<<"     ";
            }
            else
            {
                myNode* imTemp=first;
                while (iposition-1)
                {
                    imTemp=imTemp->link;
                    iposition--;
                }
                n.link=imTemp->link;
                n.data=value;
                imTemp->link=&n;
            }
        }
    }//end insertNode
    void showNode(){
        myNode* sTemp=first;
        while (sTemp != NULL)
        {
            cout<<sTemp->data<<"   ";
            sTemp=sTemp->link;
        }
    }//end showNode
};
int main(){
    myNode a;
    a.insertNode(10,1);
    a.insertNode(20,2);
    a.insertNode(25,3);
    a.insertNode(30,4);
    a.insertNode(40,5);

    a.showNode();

system("pause");
}

首先你必须声明myNode *n = new myNode();,因为你在insertNode函数之外使用它。其次,您可能希望检查第一个节点是否存在,以避免在除1以外的任何其他位置插入第一个节点时出现错误(在您的情况下,如果您的第一个插入将像inserNode(x, y != 1)一样抛出错误,因为您试图访问第一个节点(这里:while (temp->link != NULL))),但该节点不存在。
这是我认为你想要的:

void insertNode(int value, int iposition){
    myNode *n = new myNode();
    myNode *cur = first;
    //insert first node or on first position
    if (cur == NULL || iposition <= 1) {
        n->data = value;
        if (cur == NULL) { //first node
            n->link = NULL;
        }
        else { //first position
            n->link = first;
        }
        first = n;
        return;
    }
    for (int i = 1; i < iposition - 1; i++) { //find given position
        if (cur->link == NULL) { //if end
            n->data = value;
            n->link = NULL;
            cur->link = n;
            return;
        }
        cur = cur->link;
    }
    //here we are on position = iposition-1
    n->data = value;
    n->link = cur->link;
    cur->link = n;
}//end insertNode