C语言实现邻接矩阵
来源:互联网 发布:学c语言好还是java好 编辑:程序博客网 时间:2024/05/21 11:13
参考:http://ahalei.blog.51cto.com/4767671/1391988
自己实现的链接表,遇到了segment fault问题, 主要是不知道怎么初始化指针数组。不能sizeof(AdjList*)*20,而是要把整个数组进行sizeof求值。整个数组sizeof(AdjList(*)[20])的sizeof值只有8.和sizeof(AdjList(*)) 一样,证明只需要求得数组的头指针值。
AdjList (*list)[20] = (AdjList(*)[20])malloc(sizeof(AdjList(*)[20]));
#include<stdlib.h>#include<stdio.h>typedef struct _edge{ int src,dst,dist; struct _edge *next;}Edge;typedef struct _head{ struct _edge *head; struct _edge *tail;}AdjList;//virtual head,real tailvoid list_add(AdjList *list,Edge *edge){// printf("list=%p\n",list);// printf("list->head=%p\n",list->head); if(list->head->next == NULL) { list->tail = edge; list->head->next = edge;// printf("insert a new tail.src=%d|dst=%d|dist=%d|tail_next=%p\n",// list->head->src,// edge->dst,// edge->dist,// edge->next); } else { list->tail->next = edge; list->tail = edge;// printf("add to old tail.src=%d|dst=%d|dist=%d|tail_next=%p\n",// list->head->src,// edge->dst,// edge->dist,// edge->next); } }void list_travel(AdjList *list){ Edge *temp = (Edge*)malloc(sizeof(Edge)); temp = list->head->next; printf("adjacency node:%2d | ",list->head->src); while(temp != NULL) { printf("%2d->%2d=%2d ",list->head->src,temp->dst,temp->dist); temp = temp->next; } printf("\n");}void main(){ //vertext array //printf("edge_size = %d;Adj_size=%d\n",(int)sizeof(Edge),(int)sizeof(AdjList(*)[20])); AdjList (*list)[20] = (AdjList(*)[20])malloc(sizeof(AdjList(*)[20])); int i,j,k,vNum,eNum,dist; printf("Please input num of vertex and edge\n"); scanf("%d %d",&vNum,&eNum); //init Edge,start from 1 for(i=1;i<=vNum;i++) { list[i]->head = (Edge*)malloc(sizeof(Edge)); list[i]->tail = (Edge*)malloc(sizeof(Edge)); list[i]->head->src = i; list[i]->head->next = list[i]->tail = NULL; } printf("Please input edge info:i j dist\n"); for(k=1;k<=eNum;k++) { Edge *edge = (Edge*)malloc(sizeof(Edge)); scanf("%d %d %d",&i,&j,&dist); edge->dst = j; edge->dist = dist; edge->next = (Edge*)malloc(sizeof(Edge)); edge->next = NULL; list_add(list[i],edge); } //travel the list printf("******************************\n"); printf("adjacency matrix is like this:\n"); for(k=1;k<=vNum;k++) { list_travel(list[k]); } }
运行结果
norton@norton-laptop:~/learning/sample code/algrithm/map/adjacency_list$ ./adjacency.oedge_size = 24;Adj_size=8
Please input num of vertex and edge
5 10
Please input edge info:i j dist
1 2 10
2 3 20
3 4 5
4 5 15
5 1 20
5 2 13
1 5 8
2 4 9
3 5 7
4 2 3
******************************
adjacency matrix is like this:
adjacency node: 1 | 1-> 2=10 1-> 5= 8
adjacency node: 2 | 2-> 3=20 2-> 4= 9
adjacency node: 3 | 3-> 4= 5 3-> 5= 7
adjacency node: 4 | 4-> 5=15 4-> 2= 3
adjacency node: 5 | 5-> 1=20 5-> 2=13
0 0
- C语言实现邻接矩阵
- C语言邻接矩阵的实现
- 图的邻接矩阵(C语言实现)
- [数据结构]图邻接矩阵C语言简单实现
- 图的邻接矩阵存储(C语言实现)
- 基于邻接矩阵的Prim算法(C语言实现)
- 图的邻接矩阵的建立(C语言实现)
- Prim算法的C语言实现(邻接矩阵)
- C语言实现图的邻接矩阵和BFS DFS
- c语言实现图的基本操作--邻接矩阵存储
- 图邻接矩阵的c语言实现 graph_adj_matrix.cpp
- 复习(数据结构):图:c语言:邻接矩阵
- 拓扑排序(C语言 邻接矩阵存储)
- 图的邻接矩阵(C语言)
- 图的邻接矩阵存储 深度优先遍历 广度优先遍历 C语言实现
- 图邻接矩阵存储 最小生成树 prim普里姆算法 C语言实现
- 数据结构之---C语言实现图的数组(邻接矩阵)存储表示
- c语言:图的邻接矩阵的建立与广度优先搜索实现
- Open TSDB 中文指南
- 第六周作业1——利用哈夫曼编码英文字母表
- 如何用电脑做服务器
- dedecms联动模型支持dede_sys_enum的father字段自动追加内容
- ProGuard混淆器使用
- C语言实现邻接矩阵
- AR模型方法
- Pig系统分析(4)-Logical Plan Optimizer
- wp项目从线上拷贝下来本地链接问题
- python中,多模块工程,修改另一模块的变量的方法
- 11384 Help is needed for Dexter
- 技能冷却图标实现(原理篇)
- 在IOS代码中使用UNIX命令
- 阿里校招二面总结