图的创建
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)尽量写小函数,小函数构建大函数,方便阅读和调试
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)尽量写小函数,小函数构建大函数,方便阅读和调试
相关文章推荐
- 屏幕适配全攻略
- 屏幕适配全攻略
- java中的String,StringBuffer,StringBuilder
- 屏幕适配全攻略
- 关于有时候插件加载不出来的情况
- WIN7 (64 位)安装AutoCAD2012失败解决方法
- tomcat发布web项目中文乱码
- 《Monkey Android》第2课之环境搭建
- Combination Sum I/II - Leetcode
- 使用异步Get在新浪微博上获取最新的微博
- uva548解题报告
- 一个值得珍藏的网站
- log4j sql
- Java自学之路---DotCom
- 硬盘中内存芯片
- 利用axis生成wsdl服务端代码
- 使用Playground编写第一个Swift程序
- 12月29日-Java的历史和发展方向
- 网络字节顺序
- ssh免密码登录