设为首页 加入收藏

TOP

一步一步写算法(之图创建) (一)
2014-11-23 23:40:04 来源: 作者: 【 】 浏览:32
Tags:步一步 算法 创建

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

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

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;

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; 为了创建图,首先我们需要创建节点和创建边。不妨从创建节点开始,

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;

}

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;

}

接着应该创建边了,

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;

}

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;

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

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;

}

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相关呢?其实也不难。

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;

}

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;

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

VECTEX* find_vectex_in_graph(VECTEX* pVectex, int start)

{

if(NULL == pVectex)

return NULL;

while(pVectex){

if(

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇一步一步写算法(之“数星星”) 下一篇一步一步写算法(之图结构)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: