C 将结构元素链接和指定给随机节点
这就是问题所在: 基本上,您将得到一个包含以下内容的大文件(想想20000多行文本): 第1行:C 将结构元素链接和指定给随机节点,c,algorithm,C,Algorithm,这就是问题所在: 基本上,您将得到一个包含以下内容的大文件(想想20000多行文本): 第1行: 其余的: 您的目标是找出一种内存效率高的方法来创建结构,该结构允许您将节点与其连接的节点相关联。这些连接是双向的 应该允许用户输入任何位置/节点,并且程序必须将节点的所有邻居以及从输入节点到每个邻居的相应成本输出 *注意:大文件包含25000行这种格式的文本 integer integer integer 以下是假定的输出: Number of nodes is 25000 Enter a no
其余的:
您的目标是找出一种内存效率高的方法来创建结构,该结构允许您将节点与其连接的节点相关联。这些连接是双向的 应该允许用户输入任何位置/节点,并且程序必须将节点的所有邻居以及从输入节点到每个邻居的相应成本输出 *注意:大文件包含25000行这种格式的文本
integer integer integer
以下是假定的输出:
Number of nodes is 25000
Enter a node to examine: 0
Neighbors of node 0 are:
Node 1 with cost 450
Node 100 with cost 1130
Enter a node to examine:0
Neighbors of node 0 are:
Node 4 with cost 850
Node 91 with cost 304
Enter a node to examine: 0
Neighbors of node 0 are:
Node 3 with cost 230
Node 11 with cost 330
Enter a node to examine: exit
以下是我尝试过的:
#include <stdio.h>
#include <stdlib.h>
struct graph {
int src;
int dest;
int cost;
}PathEdge;
int main()
{
int nlines, i,v;
char r;
int ecost,ebegin, eend;
scanf(“%s”,&nlines);
PathEdge[nlines + 1];
for(i = 0; i < nlines; i++) {
scanf("%d, %d, %d\n", &ebegin, &eend, &ecost);
edges[i].begin = ebegin;
edges[i].end = eend;
edges[i].cost = ecost;
struct town
{
struct town *north;
int name[25000];
};
struct town *root, *current;
root = (struct town *) malloc (sizeof (struct town));
root->north = NULL;
strcpy (root->name,ecost);
current = (struct town *) malloc (sizeof (struct town));
current->north = root;
strcpy (current->name, ecost);
strcpy(ecost,”Node %d”,i);
}
printf(“%d”,sizeof(int name));
printf(“Please enter a node that you want to examine. If you want to exit, please press ‘X’.\n”);
scanf(“%c”,&r);
switch(r)
{
case ‘X’:
case ‘x’:
printf(“You entered a wrong value. Gomen. Try again.\n”);
break;
default:
if(0<r<25000)
{
printf(“You have accessed node %d\n”,r);
printf(“Its neighboring nodes are %d\n %d\n”,ecost[nlines+1],ecost[nlines+2]);
}
else
{
printf(“Invalid input again. Please do try again. Thanks\n”);
}
break;
}
return 0;
}
#包括
#包括
结构图{
int-src;
int dest;
国际成本;
}路径边缘;
int main()
{
内线,i,v;
字符r;
国际经济合作组织,埃比金,eend;
scanf(“%s”和“&nlines”);
路径边缘[nlines+1];
对于(i=0;i北=空;
strcpy(根->名称,ecost);
当前=(结构城镇*)malloc(规模(结构城镇));
当前->北=根;
strcpy(当前->名称,ecost);
strcpy(ecost,“节点%d”,i);
}
printf(“%d”,sizeof(int name));
printf(“请输入要检查的节点。如果要退出,请按“X”。\n”);
scanf(“%c”、&r);
开关(r)
{
案例“X”:
案例“x”:
printf(“您输入了错误的值。Gomen。请重试。\n”);
打破
违约:
如果(0),那么您的尝试有什么问题?欢迎使用堆栈溢出!寻求调试帮助的问题(“为什么此代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅:.strcpy(ecost,“Node%d”,i);
这不起作用。请了解sprintf()
,甚至更好:snprintf()
“创建允许您将节点与其连接的节点相关联的结构。这些连接是双向的。”对我来说,创建一个双链接列表(或参考前一个/下一个成员的类似迭代结构)因此,用户请求任何节点,然后您就可以向邻居提供成本等。也不要强制执行malloc
(即(struct town*))malloc
的返回,malloc
应该是malloc
)。感谢您的帮助!