您的位置:首页 > 其它

图的创建

2015-12-29 20:17 393 查看
/*******************

1    struct:   LINE  VECTEX  GRAPH

2  create_new_vectex(int start)

3  create_new_line(int end, int weight)

4
  create_new_graph(int start, int end, int weight)

5
  create_new_vectex_for_graph(int start, int end, int weight)
 

6
   find_vectex_in_graph(VECTEX* pVectex, int start)

7
   find_vectex_in_graph(VECTEX* pVectex, int start)

*******************/

前面我们讨论过图的基本结构是什么样的。它可以是矩阵类型的、数组类型的,当然也可以使指针类型的。当然,就我个人而言,比较习惯使用的结构还是链表指针类型的。本质上,一幅图就是由很多节点构成的,每一个节点上面有很多的分支,仅此而已。为此,我们又对原来的结构做了小的改变:

[cpp] view
plaincopy

typedef struct _LINE  

{  

    int end;  

    int weight;  

    struct _LINE* next;  

}LINE;  

  

typedef struct _VECTEX  

{  

    int start;  

    int number;  

    LINE* neighbor;  

    struct _VECTEX* next;  

}VECTEX;  

  

typedef struct _GRAPH  

{  

    int count;  

    VECTEX* head;  

}GRAPH;  

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

[cpp] view
plaincopy

VECTEX* create_new_vectex(int start)  

{  

    VECTEX* pVextex = (VECTEX*)malloc(sizeof(VECTEX));  

    assert(NULL != pVextex);  

  

    pVextex->start = start;  

    pVextex->number = 0;  

    pVextex->neighbor = NULL;  

    pVextex->next = NULL;  

    return pVextex;  

}  

    接着应该创建边了,

[cpp] view
plaincopy

LINE* create_new_line(int end, int weight)  

{  

    LINE* pLine = (LINE*)malloc(sizeof(LINE));  

    assert(NULL != pLine);  

      

    pLine->end = end;  

    pLine->weight = weight;  

    pLine->next = NULL;  

    return pLine;  

}  

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

[cpp] view
plaincopy

VECTEX* create_new_vectex_for_graph(int start, int end, int weight)  

{  

    VECTEX* pVectex = create_new_vectex(start);  

    assert(NULL != pVectex);  

      

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

    assert(NULL != pVectex->neighbor);  

      

    return pVectex;  

}  

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

[cpp] view
plaincopy

GRAPH* create_new_graph(int start, int end, int weight)  

{  

    GRAPH* pGraph = (GRAPH*)malloc(sizeof(GRAPH));  

    assert(NULL != pGraph);  

  

    pGraph->count = 1;  

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

    assert(NULL != pGraph->head);  

  

    return pGraph;  

}  

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

[cpp] view
plaincopy

VECTEX* find_vectex_in_graph(VECTEX* pVectex, int start)  

{  

    if(NULL == pVectex)  

        return NULL;  

  

    while(pVectex){  

        if(start == pVectex->start)  

            return pVectex;  

        pVectex = pVectex->next;  

    }  

  

    return NULL;  

}  

  

LINE* find_line_in_graph(LINE* pLine, int end)  

{  

    if(NULL == pLine)  

        return NULL;  

  

    while(pLine){  

        if(end == pLine->end)  

            return pLine;  

  

        pLine = pLine->next;  

    }  

  

    return NULL;  

}  

总结:

    (1)图就是多个链表的聚合

    (2)想学好图,最好把前面的链表和指针搞清楚、弄扎实

    (3)尽量写小函数,小函数构建大函数,方便阅读和调试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: