没有实现的动态创建数组_PTA_063

博客探讨了在编程中尝试动态创建数组时遇到的问题。作者在定义图的代码段中试图实现这一功能,但经过尝试,修改未达到预期效果。问题留存待明日继续研究解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在写图的定义时

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;
		}
	}
}

先保存下来明天再考虑

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值