将图读进邻接表实现文件C语言
2010-12-08 00:46
363 查看
/* adjacenty_list.c -- 邻接表实现文件 */ #include <stdio.h> #include <stdlib.h> #include "adjacenty_list.h" /* 局部函数声明 */ Vertex * Make_Vertex (const Name vertex) ; /* 接口函数定义 */ int CreateAdjacent_List (Adjacent_List * const padj, const int capacity) { int lenth_list = sizeof (Vertex), lenth_indegree = sizeof (int) ; if (capacity <= 0) return 0 ; *padj = (struct adjacency_list *) malloc (sizeof (struct adjacency_list)) ; if (NULL == *padj) return 0 ; (*padj) -> list = (Vertex *) malloc (lenth_list * capacity) ; if (NULL == (*padj) -> list) { free (*padj) ; return 0 ; } (*padj) -> indegree = (int *) malloc (lenth_indegree * capacity) ; if (NULL == (*padj) -> indegree) { free ((*padj) -> list) ; free (*padj) ; return 0 ; } (*padj) -> capacity = capacity ; return 1 ; } int InitializeALineOfAdjacent_List (const Adjacent_List * const padj, const int index, const Name name, const int indegree, const int sub, ...) { va_list ap ; Vertex * temp ; Name others ; int i ; if (index >= (*padj) -> capacity || sub < 0) return 0 ; va_start (ap, sub) ; (*padj) -> indegree[index] = indegree ; (*padj) -> list[index].name = name ; if (0 == sub) { (*padj) -> list[index].next = NULL ; va_end (ap) ; return 1 ; } else { others = va_arg (ap, Name) ; temp = (*padj) -> list[index].next = Make_Vertex (others) ; if (NULL == temp) { va_end (ap) ; return 0 ; } for (i = 1; i < sub; i++) { others = va_arg (ap, Name) ; temp -> next = Make_Vertex (others) ; temp = temp -> next ; } va_end (ap) ; return 1 ; } } void PrintAdjacent_List (const Adjacent_List * const padj) { Vertex scan ; int i, capacity ; capacity = (*padj) -> capacity ; for (i = 0; i < capacity; i++) { printf ("Indegree:%-2d", (*padj) -> indegree[i]) ; scan = (*padj) -> list[i] ; while (scan.next) { printf ("%c ", scan.name) ; scan = *scan.next ; } printf ("%c ", scan.name) ; putchar ('/n') ; } } void ReleaseForAdjacent_List (const Adjacent_List * const padj) { Vertex * scan, * temp ; int i, capacity ; capacity = (*padj) -> capacity ; for (i = 0; i < capacity; i++) { scan = (*padj) -> list[i].next ; while (scan) { temp = scan ; scan = scan -> next ; free (temp) ; } } free ((*padj) -> list) ; free ((*padj) -> indegree) ; free (*padj) ; } /* 局部函数定义 */ Vertex * Make_Vertex (const Name vertex) { Vertex * new_vertex = (Vertex *) malloc (sizeof (Vertex)) ; if (new_vertex) { new_vertex -> name = vertex ; new_vertex -> next = NULL ; } return new_vertex ; }
相关文章推荐
- 邻接表生成模型实现文件C语言
- 图的邻接表存储 深度优先遍历 广度优先遍历 C语言实现
- 用C语言实现一个自己的文件拷贝工具
- C语言二进制文件读写程序实现(基础)
- C语言实现文件内容按行随机排列的算法示例
- C语言实现连接多个文件
- 用Linux_C语言模拟CP命令,实现文件和文件夹的拷贝
- 不相交集ADT实现文件C语言
- c语言实现文件拷贝命令
- 使用C语言实现从一段pcm格式音频文件中截取一段数据
- C语言实现读取FAT12文件系统
- c语言实现最简单的写文件操作
- 斐波那契堆实现文件C语言
- C语言文件读写注册登录功能实现
- 配合Dijkstra算法的二叉堆实现文件C语言
- C语言拷贝文件函数实现(linux cp [source file] [target file] 命令的实现)
- C语言实现文件按行倒序存储
- Ubuntu下C语言实现以子进程号命名的文件及文件夹的新建、删除等操作。
- C语言实现修改文本文件中的特定行
- C语言实现的计算一个文件下的所有文件的总大小(还有BUG,如果文件夹下面还有文件家就有问题,等待更新)