创建邻接单链表
我有类似图形的 ADT:
typedef struct element {
int info;
struct element *link;
} Tnode;
typedef struct graphAdjList {
int nodes;
Tnode *adjList[MAX]; // array of 20 pointers to Tnode
} Tgraph;
Tgraph *readGraph(FILE *fd);
void printGraph(Tgraph *g);
void dfs(Tgraph *g, int start, int visited[], int pred[]);
void destroyGraph(Tgraph *g);
并附上包含以下内容的文件“maze.txt”:
0 1 6 8
1 0 2 3
2 10 11
3 1 4 12
4 3 13
5 4 6 9
6 5 7
7 8 9
8 0 7 14
9 15 5 7
10 2
11 2
12 3
13 4
14 8
15 9
其中 0 1 6 8 表示节点号 0 与节点 1、6 和 8 有(单向)连接。现在我真的不知道如何通过 readGraph() 方法根据上面的列表构建图表。你们能否指出我的详细实现,因为我是 C 新手?多谢
I have ADT for graph like:
typedef struct element {
int info;
struct element *link;
} Tnode;
typedef struct graphAdjList {
int nodes;
Tnode *adjList[MAX]; // array of 20 pointers to Tnode
} Tgraph;
Tgraph *readGraph(FILE *fd);
void printGraph(Tgraph *g);
void dfs(Tgraph *g, int start, int visited[], int pred[]);
void destroyGraph(Tgraph *g);
and enclosing file "maze.txt" with following content:
0 1 6 8
1 0 2 3
2 10 11
3 1 4 12
4 3 13
5 4 6 9
6 5 7
7 8 9
8 0 7 14
9 15 5 7
10 2
11 2
12 3
13 4
14 8
15 9
where 0 1 6 8 means node number 0 has (one directional) connections to nodes 1, 6 and 8. Now I don't really know how to construct graph based on above list through method readGraph(). Could you guys please point me out detailed implementation cause i'm newbie in C ? Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您的老师打算让您将图形读入邻接列表。
这是 C99 中的示例实现。
将其保存到名为
maze.c
的文件中,然后使用(例如)进行编译:gcc -g -O2 -Wall -Werror -std=c99 -o maze maze.c
我没有实现 dfs() 函数,因为我并不完全清楚它应该做什么。我假设您的作业中有一些文字解释了每个功能的要求。您可能还需要重写
printGraph()
函数才能满足作业要求。It looks like your teacher intends for you to read the graph into an adjacency list.
Here is a sample implementation in C99.
Save it to a file named
maze.c
and then compile it with (for example):gcc -g -O2 -Wall -Werror -std=c99 -o maze maze.c
I didn't implement the
dfs()
function as it wasn't entirely clear to me what it's supposed to do. I'm assuming there is some text that goes along with your assignment explaining the requirements of each function. You will probably have to rewrite theprintGraph()
function to match the assignment requirements as well.