如何修复链接列表中的“读取访问冲突”

时间:2019-05-15 22:52:11

标签: c++ graph

我已经使用LinkedList实现了无向图。我的问题是,每个LinkedList函数都收到读取acces冲突(p为0xCDCDCDCDCD)。

class LinkedList {

public:

    Node *start;
    LinkedList() { start = NULL; } //default constructor

    //---------------------------------

    ~LinkedList() //destructor

    {

        Node *temp;
        while (start)

        {

            temp = start->next;
            delete start;
            start = temp;

        }

    }
    //----------------------------------
    //get the address of first node of the linked list

    Node* getStart() { return start; }

    //---------------------------------
    //insert an element into linked list

    void insert(int data)

    {

        Node *ptr = new Node(data);
        if (start == NULL)

        {

            start = ptr;

        }

        else

        {

            Node *temp = start;

            while (temp->next)

                temp = temp->next;

            temp->next = ptr;

        }

    }

    //---------------------------------
    bool findNode(int data) {
        Node *p = start;
        while (p)

        {

            if (p->info == data)
                return true;

            p = p->next;

        }
        return false;
    }


};

然后我实现了这样的Graph类:

class Graph {

private:
    LinkedList *A;
    int vertex;
    int edges;

public:
    Graph(int vertex) {
        this->vertex = vertex;
        edges = 0;
        A = (LinkedList *)malloc(sizeof(LinkedList)*vertex);
    }
    void insertEdge(int u, int v) {
        if (A[u].findNode(v) == false) {
            A[u].insert(v);
            A[v].insert(u);
            edges++;
        }
    }
};

和节点:

class Node {
    friend class LinkedList;
    int info;
    Node *next;
    Node() //default constructor
    {
        info = 0;
        next = 0;
    }
    Node(int item) //parameterized constructor

    { 
        info = item;
        next = 0; 
    }

};

主要:

int main()

{
    Graph G(7);
    G.insertEdge(1, 3);
    G.insertEdge(4, 6);
    G.displayGraph();
    return 0;
}

我不确定到底发生了什么,但是,每次尝试调试它时,都会把我带到指针函数。

0 个答案:

没有答案
相关问题