将struct元素链接并分配给随机节点

时间:2015-04-29 12:14:36

标签: c algorithm

这是问题所在:

  

基本上,您将获得一个包含以下内容的大文件(认为20000多行文本):   第1行:<number_of_nodes> <number of edges>
  其余的:<Source> <Destination> <Cost>
  您的目标是找出一种内存有效的方法来创建允许您将节点关联到其连接节点的结构。这些连接是双向的。

     

应该允许用户输入任何位置/节点,程序必须输出节点的所有邻居以及从输入节点到每个邻居的相应成本。

*注意:那个|大文件|包含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;
}

0 个答案:

没有答案