错误:异常:抛出未处理的异常:读取访问冲突。 temp是0xDDDDDDDD

时间:2018-06-07 05:14:45

标签: c++ linked-list

我是初学者,正在处理链接列表。我正在尝试创建一个程序,它将元素添加到列表中,更新列表,显示它并删除它。我得到一个例外:读取访问冲突。 temp是0xDDDDDDDD。 我认为display()函数存在一些问题。调试器也显示相同的内容。

 #include "stdafx.h"
    #include "Node.h"
    #include<iostream>
    using namespace std;

    Node::Node() //constructor
    {
        head = NULL;
    }

    Node::~Node()    //destructor
    {
    }

    void Node::addFirstNode(int n)    //adding the first element in the list
    {
        node *temp = new node;
        temp->data = n;
        temp->next = NULL;
        head = temp;    
    }

    void Node :: addLast(int n)    //Adding elements at the end of the list
    {
        node *last = new node;
        last->data = n;
        last->next = NULL;
        node *temp = new node;

        temp = head;        
        while (temp->next != NULL) {
            temp = temp->next;
        }       
        temp->next = last;  
    }

    void Node::display()      //Displaying the list
    {
        node *temp = head;
        while (temp != NULL)
        {
            cout<<temp->data;
            temp = temp->next;
        }
    }


//the main function:

#include "stdafx.h"
#include "Node.h"
#include<iostream>
using namespace std;

int main()
{
    Node a;
    a.addFirstNode(101);    //Calling function : addFirstNode

    a.addLast(102);         //Calling function : addLast

    a.addLast(103);       //Calling function : addLast

    a.addLast(104);       //Calling function : addLast

    a.display();         //Calling function : display
    return 0;
}

Node.h文件如下:

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

class Node
{
private :
    node *head;

public:
    Node();
    ~Node();
    void addFirstNode(int n);
    void addLast(int n);    
    void display(); 
};

1 个答案:

答案 0 :(得分:0)

您应该重命名List以更好地描述它是什么,例如Node::addFirst()

temp->next = NULL;中,将temp->next = head;替换为Node每次在Node::addLast()添加node *temp = new node;时,您都不想丢弃列表。

node *temp = head;中,将Node替换为train_qty = np.array([[100,200,300,110,200,300,1000,1500,3700]]).reshape(-1,1) train_prices = np.array([[1000,1800,300,110,200,300,1000,1500,3700]]).reshape(-1,1) 每次在split(image, Bands); 末尾添加image时,都不希望泄漏内存。

相关问题