美文网首页
C++算法之图的结构的代码

C++算法之图的结构的代码

作者: we1212 | 来源:发表于2021-10-27 14:40 被阅读0次

    下面资料是关于C++算法之图的结构的代码,应该能对码农有帮助。

    typedef struct _LINE 

        int end; 

        int weight; 

    }LINE; 

     

    typedef struct _VECTEX 

        int start; 

        int number; 

    }VECTEX; 

     

    typedef struct _GRAPH 

        int count; 

    }GRAPH; 

                                   

                           

                                   

    为了创建图,首先我们需要创建节点和创建边。不妨从创建节点开始,

                                   

                           

                                   

        assert(NULL != pVextex); 

     

        pVextex->start = start; 

        pVextex->number = 0; 

        pVextex->neighbor = NULL; 

        pVextex->next = NULL; 

        return pVextex; 

                                   

                           

                                   

    接着应该创建边了,

                                   

                           

                                   

        assert(NULL != pLine); 

         

        pLine->end = end; 

        pLine->weight = weight; 

        pLine->next = NULL; 

        return pLine; 

                                   

                           

                                   

    有了上面的内容,那么创建一个带有边的顶点就变得很简单了,

                                   

                           

                                   

        assert(NULL != pVectex); 

         

        pVectex->neighbor = create_new_line(end, weight); 

        assert(NULL != pVectex->neighbor); 

         

        return pVectex; 

                                   

                           

                                   

    那么,怎么它怎么和graph相关呢?其实也不难。

                                   

                           

                                   

        assert(NULL != pGraph); 

     

        pGraph->count = 1; 

        pGraph->head = create_new_vectex_for_graph(start, end, weight); 

        assert(NULL != pGraph->head); 

     

        return pGraph; 

                                   

                           

                                   

    有了图,有了边,那么节点和边的查找也不难了。

                                   

                           

                                   

        if(NULL == pVectex) 

            return NULL; 

     

        while(pVectex){ 

            if(start == pVectex->start) 

                return pVectex; 

            pVectex = pVectex->next; 

        } 

     

        return NULL; 

     

        if(NULL == pLine) 

            return NULL; 

     

        while(pLine){ 

            if(end == pLine->end) 

                return pLine; 

     

            pLine = pLine->next; 

        } 

     

        return NULL; 

                                   

                           

                                   

    总结:(1)图就是多个链表的聚合(2)想学好图,最好把前面的链表和指针搞清楚、弄扎实(3)尽量写小函数,小函数构建大函数,方便阅读和调试

                                   

                           

                   

                   

               

               

                   

    相关文章

      网友评论

          本文标题:C++算法之图的结构的代码

          本文链接:https://www.haomeiwen.com/subject/aehpaltx.html