使用链接列表的图表示 [英] Graph Representation using Linked-List

查看:131
本文介绍了使用链接列表的图表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试弄清楚在向某个成对顶点添加边时如何正确获取指针时遇到了一些麻烦。

Im having some trouble trying to figure out how to get the pointers right when adding edges to a certain paired vertex.

下面是关于如何链接列表应该看起来像顶点和节点完成后被输入。

Below is a short idea about how the linked list should look like after Vertexs and Nodes are done being Inputed.

如何保持订单在neighborList上呢?如果在当前顶点已经有顶点边缘,是否还有另一个条件?

How can i keep order on the neighborList as well? Should there be another condition if there is already a vertex edge in that current vertex?

试图构建结构化类:

class graph{
private:
    typedef struct node{
        char vertex;
        node * nodeListPtr;
        node * neighborPtr;

    }* nodePtr;
    nodePtr head;
    nodePtr curr;
public:
    graph();
    ~graph();

    void AddNode(char AddData);
    void AddEdge(char V, char E);
    void printList();
};

graph::graph(){
    head = NULL;
    curr = NULL;
}

// Adds a node to a linked list
void graph::AddNode(char AddData){
    nodePtr n = new node;
    n->nodeListPtr = NULL;
    n->vertex = AddData;

    if(head != NULL){
        curr = head;
        while(curr->nodeListPtr != NULL){
            curr = curr->nodeListPtr;
        }
        curr->nodeListPtr = n;
    }
    else{
        head = n;
    }
}

// takes 2 Parameters (V is pointing to E)
// I want to set it up where the neighborptr starts a double linked List basically
void graph::AddEdge(char V, char E){
    // New Node with data
    nodePtr n = new node;
    n->neighborPtr = NULL;
    n->vertex = E;
    // go to the first node in the nodeList and go through till you reach the Vertex V
    curr = head;
    while(curr->vertex != V){
        curr = curr->nodeListPtr;
    }
    //Once the Vertex V is found in the linked list add the node to the neighborPtr.
    curr->neighborPtr = n;

}

推荐答案

目前有一个问题是每个节点只能有一个边缘节点。在你的插图中,节点A,C和D都是可能的,但是节点B不是没有什么不同。

One problem you currently have is that each node can only have one "edge" node. In your illustration, nodes A, C, and D are all possible, but node B is not without doing things a little differently.

问题出现在这里:

curr->neighborPtr = n;

每次调用AddEdge()到同一个顶点时,它都会直接覆盖该顶点的neighborPtr。

Every time you call AddEdge() to the same vertex, it will simply overwrite that vertex's neighborPtr. You make no effort to traverse the neighborPtrs until you find a null pointer.

考虑添加另一个while循环以递归添加边:

Consider adding another while loop for adding edges recursively:

while (curr->neighborPtr != null)
    curr = curr->neighborPtr;
curr->neighborPtr = n;

请注意,这不是代码中唯一的问题;你有几个地方,你应该防止空指针,而不是。例如:在AddEdge()中,如果找不到顶点V,会发生什么?您的行为假设它已经创建。如果没有,你会得到一些空指针错误。如果您想要使代码在功能上更加强大,请记住这一点。

Note that this is not the only issue in your code; you have a few places where you should be guarding against null pointers and are not. For example: in AddEdge(), what happens if the vertex V cannot be found? You are acting under the assumption that it has already been created. If it hasn't, you will end up with some null pointer errors. Keep this in mind if you are trying to make code that is robust in addition to being functional.

这篇关于使用链接列表的图表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆