我正在做一项大学作业,为了完成作业,我需要一个程序来模拟用邻接列表表示的图的深度优先搜索。说清楚;任务不是制作程序,而是我需要使用不同的图形运行程序,并记录有关这些图形的数据。我班级的教科书显示了我需要组合在一起的各种程序,以便创建这个 DFS 实现,但没有一个程序可以自行编译。
我相信我已经接近制定一个可行的计划了。但是,当我尝试编译时,仍然出现 1 个错误;错误是:“错误:使用不完整的返回类型“struct ptrEdge”调用“EDGE””,位于“Program18.2.c”(我的主程序)的第 23 行。这是我的代码,它分为两个文件:
程序18.2.c:
#include <stdio.h>
#include "GRAPH.h" // Include the appropriate header file for Graph and Edge
//using namespace std;
static int cnt;
typedef struct STnode* link;
// Declare prev, Graph, and Edge.
int prev[MAX_V]; // Assuming MAX_V is the maximum number of vertices
typedef struct Graph* Graph; // Define the Graph structure appropriately
typedef struct Edge* ptrEdge; // Define the Edge structure appropriately
void dfsR(Graph G, ptrEdge e);
void dfsR(Graph G, ptrEdge e) {
link t;
int w = e->w; // Assuming 'w' is a member of the Edge structure
prev[w] = cnt++;
for (t = G->adj[w]; t != NULL; t = t->next) {
if (prev[t->v] == -1) {
dfsR(G, EDGE(w, t->v)); // Assuming EDGE is a constructor for an Edge structure
}
}
}
int main() {
// Initialize the 'prev' array, 'cnt', and create a Graph and an Edge.
// Call dfsR with the appropriate parameters.
return 0;
}
GRAPH.h:
#ifndef GRAPH_H
#define GRAPH_H
// Define the maximum number of vertices in the graph
#define MAX_V 100
// Define the Graph structure
struct Graph {
int V; // Number of vertices
int E; // Number of edges
int** adj; // Adjacency matrix, you may use an adjacency list or a different representation
};
// Define the Edge structure
struct Edge {
int v; // Source vertex
int w; // Destination vertex
// Add other relevant members as needed
};
struct STnode {
int v;
struct STnode* next;
};
// Define a constructor for Edge
struct ptrEdge EDGE(int v, int w);
#endif
不久前,我在 Program18.2.c 中将“Edge”的结构体定义为“typedef struct Edge* Edge;”,以及在 GRAPH.h 中对构造函数的定义。我还在 Program18.2 的第 14 行和第 16 行引用了“ptrEdge”对象作为“Edge”对象。
编译这段代码也在第 23 行给了我一个错误,尽管略有不同:“将 'struct Edge' 传递给不兼容类型 'Edge'(又名 'struct Edge*')的参数”。
因此,我尝试按照上面描述的方式将 Edge 定义更改为 ptrEdge,并且认为编译器对如此多的“Edge”引用感到困惑。然而,情况似乎并非如此,我现在遇到了我在帖子顶部提到的错误。
有谁知道如何解决该错误并编译该程序?如有任何反馈,我们将不胜感激!
您将
EDGE()
函数定义为返回 struct ptrEdge
。
但是,您对
ptrEdge
的定义是:
typedef struct Edge* ptrEdge;
这意味着
ptrEdge
是 struct Edge *
的别名,因此它独立存在:
ptrEdge EDGE(int v, int w);