这是我从零开始实现邻接列表的代码。我想从专家那里得到优化的反馈。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*a single node of an adjacency list*/
typedef struct adjList{
int dest;
struct adjList *next;
} adjList;
/*Image of a graph...*/
typedef struct Image{
int source;
adjList *head;
} Image;
void initialize_graph(Image *graph, int vertices);
void print_graph(Image graph[], int vertices);
void add_adj_node(Image *graph, int source, int destiny, bool directed);
void free_graph(Image graph[], int vertices);
int main() {
int vertices;
scanf("%d", &vertices);
Image graph[vertices];
printf("size of graph: %d bytes\n", sizeof graph);
initialize_graph(graph, vertices);
printf("initialized image of graph\n");
print_graph(graph, vertices);
printf(" \n");
//is the graph directed ? ans: false
add_adj_node(graph, 1, 7, false);
add_adj_node(graph, 1, 3, false);
add_adj_node(graph, 4, 6, false);
add_adj_node(graph, 4, 1, false);
add_adj_node(graph, 5, 2, false);
add_adj_node(graph, 1, 5, false);
add_adj_node(graph, 1, 2, false);
print_graph(graph, vertices);
free_graph(graph, vertices);
/*if this print produces segmentation fault then the memory is fully freed*/
printf("graph[1].head->dest%d\n", graph[1].head->dest);
return 0;
}
void initialize_graph(Image graph[], int vertices) {
for(int i = 1; i<= vertices; i++){
graph[i].source = i;
graph[i].head = NULL;
}
return;
}
void add_adj_node(Image *graph, int src, int dest, bool directed){
adjList *cache = malloc(sizeof(adjList));
/*create a single node*/
cache->dest = dest;
cache->next = NULL;
if(graph[src].head == NULL){
graph[src].head = cache;
}
else{
/*put the head address on the crawler*/
adjList *crawler = graph[src].head;
while( crawler->next != NULL){
crawler = crawler->next;
}
/*update head value and address. head will point to new adj node
this will also link src -> dest*/
crawler->next = cache;
}
if (directed == false) {
directed = true;
/*notice we've changed the sequence. dest and src*/
add_adj_node( graph, dest, src, directed);
}
return;
}
void print_graph(Image *graph, int vertices){
for(int i = 1; i<= vertices; i++){
adjList *crawl = graph[i].head;
printf("node: %d ", graph[i].source);
while(crawl){
printf("%d ", crawl->dest);
crawl = crawl->next;
}
printf("\n");
}
return;
}
/*just a reverse version of crawling a graph*/
void free_graph(Image *graph, int vertices){
for(int i = 1; i<= vertices; i++){
adjList *cache;
printf("releasing elements of node: %d ", graph[i].source);
while(graph[i].head){
/*put the next adjacency node in the cache*/
cache = graph[i].head->next;
/*free the present adjacencey node*/
free(graph[i].head);
graph[i].head = cache;
}
printf("\n");
}
return;
}发布于 2016-11-22 19:46:17
vertices条目,从索引0开始,最大的有效索引是vertices - 1。用于(int= 1;i<=顶点;i++)的循环接触到graph[vertices],这是非法的。一个正确的(和白痴的)循环用于(int= 0;i<顶点;i++)add_adj_node是令人困惑的。我建议有一个辅助函数do_add_adj_node,并像这样调用它: void add_adj_node(图像*图,int src,int add_adj_node){do_add_adj_node(图,src,dst);do_add_adj_node(图,dst,src)};发布于 2016-11-22 20:47:53
有这么多空白行,看看你怎么会滚动这么多才能读到代码!
为了将函数中的语句组分开,或者用一两行空行分隔函数,偶尔使用空行是可以的。但在我看来,两行以上的空白处是不必要的。
下面是我将如何呈现代码。除了删除46行空白行之外,我什么也没有改变,这大约是所有原始行的四分之一!
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*a single node of an adjacency list*/
typedef struct adjList{
int dest;
struct adjList *next;
} adjList;
/*Image of a graph...*/
typedef struct Image{
int source;
adjList *head;
} Image;
void initialize_graph(Image *graph, int vertices);
void print_graph(Image graph[], int vertices);
void add_adj_node(Image *graph, int source, int destiny, bool directed);
void free_graph(Image graph[], int vertices);
int main() {
int vertices;
scanf("%d", &vertices);
Image graph[vertices];
printf("size of graph: %d bytes\n", sizeof graph);
initialize_graph(graph, vertices);
printf("initialized image of graph\n");
print_graph(graph, vertices);
printf(" \n");
//is the graph directed ? ans: false
add_adj_node(graph, 1, 7, false);
add_adj_node(graph, 1, 3, false);
add_adj_node(graph, 4, 6, false);
add_adj_node(graph, 4, 1, false);
add_adj_node(graph, 5, 2, false);
add_adj_node(graph, 1, 5, false);
add_adj_node(graph, 1, 2, false);
print_graph(graph, vertices);
free_graph(graph, vertices);
/*if this print produces segmentation fault then the memory is fully freed*/
printf("graph[1].head->dest%d\n", graph[1].head->dest);
return 0;
}
void initialize_graph(Image graph[], int vertices) {
for(int i = 1; i<= vertices; i++){
graph[i].source = i;
graph[i].head = NULL;
}
return;
}
void add_adj_node(Image *graph, int src, int dest, bool directed){
adjList *cache = malloc(sizeof(adjList));
/*create a single node*/
cache->dest = dest;
cache->next = NULL;
if(graph[src].head == NULL){
graph[src].head = cache;
}
else{
/*put the head address on the crawler*/
adjList *crawler = graph[src].head;
while( crawler->next != NULL){
crawler = crawler->next;
}
/*update head value and address. head will point to new adj node
this will also link src -> dest*/
crawler->next = cache;
}
if (directed == false) {
directed = true;
/*notice we've changed the sequence. dest and src*/
add_adj_node( graph, dest, src, directed);
}
return;
}
void print_graph(Image *graph, int vertices){
for(int i = 1; i<= vertices; i++){
adjList *crawl = graph[i].head;
printf("node: %d ", graph[i].source);
while(crawl){
printf("%d ", crawl->dest);
crawl = crawl->next;
}
printf("\n");
}
return;
}
/*just a reverse version of crawling a graph*/
void free_graph(Image *graph, int vertices){
for(int i = 1; i<= vertices; i++){
adjList *cache;
printf("releasing elements of node: %d ", graph[i].source);
while(graph[i].head){
/*put the next adjacency node in the cache*/
cache = graph[i].head->next;
/*free the present adjacencey node*/
free(graph[i].head);
graph[i].head = cache;
}
printf("\n");
}
return;
}https://codereview.stackexchange.com/questions/147816
复制相似问题