邻接列表C

时间:2016-06-28 01:37:02

标签: c pointers linked-list adjacency-list undirected-graph

我一直试图创建一个带有无向图的Adjancey List。一切都很顺利,除了我需要一个Node(或我的代码中的顶点)有多个边连接到它。 我有6个顶点A,B,C,D,E,F 并且只有A到B,A到C,A到D的边缘 this is what it looks like conceptually. 我一直在尝试提出一种将多个边连接在一起的算法,我无法弄明白。这是我的结构供参考,我的其他陈述是我需要走到链接边缘的末端并附加新边缘的地方。

typedef struct
{
    char c;
    bool isVisited;
    struct EDGETAG* p;
} VERTEX;

typedef struct EDGETAG
{
    VERTEX* v;
    struct EDGETAG* next;
} EDGE;

//~~~~~~~~
while( fgets(data,sizeof(data),filePtr) && elements < 50)
    {
        startPtr = data;
        EDGE* e  = (EDGE*)malloc(sizeof(EDGE));
        //first character
        int breakLogic = 0;
        int i;

        for( i = 0; i < 50; i++)
        {
            if(map[i].c == *startPtr)
            {
                elements = i;
                break;
            }
        }
        //first character
        map[elements].c = *startPtr;
        printf("%i %c\n",elements,map[elements].c);
        //point first character to edge
        if(map[elements].p == NULL)
        {
            map[elements].p = e;
        }
        else
        {
             /*Here is where I need help*/
        }

        startPtr += 2;

        for( i = 0; i < 50; i++)
        {
            if(map[i].c == *startPtr)
            {
                elements = i;
                breakLogic = 1;
                break;

            }

        }
        if( breakLogic == 0)
        {
            elements++;
        }
        //second character
        map[elements].c = *startPtr;
        printf("%i %c\n",elements,map[elements].c);
        //point edge to second character
        e -> v = &map[elements];
        e -> next = NULL;
        //loops at the newline

        elements++;
    }

}

0 个答案:

没有答案