在写图的定义时
typedef struct Vnode { prttoadjvnode firstedge; }adjlist[10001]; /*此处*/ /*图节点定义*/ struct Gnode { int Nv;/*顶点数*/ int Ne;/*边数*/ adjlist G; /*邻接表*/ };在这个里面,想对标记处进行动态创建,但是最终修改不成功。
下面是失败的修改版:
/*六度空间*/ /*用邻接表,BFS,队列*/ #include<stdio.h> #include<stdlib.h> /*边的定义*/ typedef struct Enode* Edge; struct Enode { int V1, V2; }; /*邻接点的定义*/ typedef struct Adjvnode* prttoadjvnode; struct Adjvnode { int adjv; prttoadjvnode next; }; /*顶点定义*/ typedef struct Vnode* adjlist; struct Vnode { prttoadjvnode firstedge; }; /*TODO:此处需修改,建议动态分配 已修改 修改失败*/ /*图节点定义*/ struct Gnode { int Nv;/*顶点数*/ int Ne;/*边数*/ adjlist * G ; /*邻接表*/ }; typedef struct Gnode* Lgraph; /*队列*/ typedef struct Qnode* ptrtoQnode; struct Qnode { int* Data;/*data数组,*/ int front, rear;/*队列头尾指针*/ int maxsize;/*容量*/ }; typedef ptrtoQnode Queue; /*图的操作*/ Lgraph creatgraph(int size); void insertedge(Lgraph graph, Edge E); Lgraph buildgraph(int N); /*队列操作*/ int AddQ(Queue Q, int x); Queue CreateQueue(int maxsize); int isempty(Queue Q); int DeleteQ(Queue Q); int SBS_BST(Lgraph graph, int s, int N, int* visited); void six(Lgraph graph, int* visited, int N); int main() { int N;/*顶点数*/ scanf("%d", &N); Lgraph graph = buildgraph(N); for (int i = 0; i < N; i++) { printf("第%d ", i); prttoadjvnode temp = graph->G[i].firstedge; while (temp) { printf("%d ", temp->adjv); temp = temp->next; } printf("\n"); } int* visited = (int*)malloc(N * sizeof(int)); for (int i = 0; i < N; i++) { visited[i] = 0; } six(graph, visited, N); return 0; } Lgraph creatgraph(int size) { Lgraph graph = (Lgraph)malloc(sizeof(struct Gnode)); graph->Nv = size; graph->Ne = 0; graph->G = (adjlist*)malloc(size * sizeof(adjlist)); for (int i = 0; i < size; i++) { graph->G[i] = (adjlist)malloc(sizeof(struct Vnode)); } for (int i = 0; i < size; i++) { graph->G[i]->firstedge = NULL; } return graph; } void insertedge(Lgraph graph, Edge E) { /*v1,v2*/ prttoadjvnode newnode = (prttoadjvnode)malloc(sizeof(struct Adjvnode)); newnode->adjv = E->V2; newnode->next = graph->G[E->V1]->firstedge; graph->G[E->V1]->firstedge = newnode; prttoadjvnode newnode1 = (prttoadjvnode)malloc(sizeof(struct Adjvnode)); newnode1->adjv = E->V1; newnode1->next = graph->G[E->V2]->firstedge; graph->G[E->V2]->firstedge = newnode1; } Lgraph buildgraph(int N) { Lgraph graph; graph = creatgraph(N); scanf("%d", &graph->Ne); if (graph->Ne != 0) { Edge E = (Edge)malloc(sizeof(struct Enode)); for (int i = 0; i < graph->Ne; i++) { scanf("%d %d", &E->V1, &E->V2); insertedge(graph, E); } } return graph; } Queue CreateQueue(int maxsize) { Queue Q = (Queue)malloc(sizeof(struct Qnode)); Q->Data = (int*)malloc(maxsize * sizeof(int)); Q->front = Q->rear = 0; Q->maxsize = maxsize; return Q; } int AddQ(Queue Q, int x) { Q->rear = (Q->rear + 1) % (Q->maxsize); Q->Data[Q->rear] = x; return 1; } int isempty(Queue Q) { return(Q->rear == Q->front); } int DeleteQ(Queue Q) { Q->front = (Q->front + 1) % Q->maxsize; return Q->Data[Q->front]; } int SBS_BST(Lgraph graph, int s,int N,int * visited) { /*以s为出发点对图进行6层BFS*/ Queue Q; Q = CreateQueue(N); int count; int level; int v, last, tail; prttoadjvnode w; visited[s] = 1; count = 1; level = 0; /*起始点定义为第0层*/ last = s; AddQ(Q, s); while (isempty(Q) != 1) { v = DeleteQ(Q); for (w = graph->G[v].firstedge; w; w = w->next) { /*访问v的每个邻接点*/ if (visited[w->adjv] == 0) { visited[w->adjv] = 1; count++; tail = w->adjv; AddQ(Q, w->adjv); } } if (last == v) { level++; last = tail; } if (level == 6)break; } return count; } void six(Lgraph graph,int *visited,int N) { int v; int count; for (v = 0; v < graph->Nv; v++) { count = SBS_BST(graph, v, N, visited); printf("%d: %.2f%%\n", v, 100.0 * (double)count / (double)graph->Nv); for (int i = 0; i < N; i++) { visited[i] = 0; } } }先保存下来明天再考虑