【问题标题】:Loop with two variables (one is pointer) in C programming在 C 编程中使用两个变量(一个是指针)循环
【发布时间】:2018-06-06 00:18:09
【问题描述】:

我有一个关于带有 2 个变量的循环的问题。
经典的一个变量i=0 > to the end of bound
第二个是*(ptr->cost),像这样使用指针显示值。

我遇到了第二个问题,实际上我无法将其放入 for 循环中。我从用户那里得到 cost 变量,我想将它们 i 和 cost variables 一起打印。但我的代码只打印 i 个变量和一个成本变量。
这是我的相关代码部分:

void print_graph(struct node *ad[],int no_of_nodes){

    struct node *ptr = NULL;
    int i,j;
    for(i=0; i<no_of_nodes; i++){
        ptr = ad[i];
        printf("\n Number %d node cost is = %d and its neighbours are :  ",i+1,*(ptr->cost));
        while(ptr != NULL){
            printf("%d\t ",ptr->data,*(ptr->cost));
            ptr = ptr->next;
        }
    }  
}

以及输出

节点 1 应该是 50,但输出显示它是 70。

我还认为成本变量不包含在 for 循环中,这就是为什么它只转一次并且顺序相反的原因。如何解决这个问题?

这是我的全部代码

struct node{
int data;
struct node *next;
int* cost;};

typedef struct node nodes;

void read_graph(struct node *ad[], int no_of_nodes);
void print_graph(struct node *ad[],int no_of_nodes);

void main()
{
    int i,j,k,nodes;
    printf("\nEnter the number of nodes :");
    scanf("%d",&nodes);

    struct node *adj[nodes];
    for(i=0; i<nodes; i++)
        adj[i] = NULL;
    read_graph(adj,nodes);
    print_graph(adj,nodes);     

}

void read_graph(struct node *ad[], int no_of_nodes){
    struct node *new_node;
    int i,j,k,val;  
    int costs[10];
    for(i=0; i< no_of_nodes; i++){
        struct node *last = NULL;

        printf("\nFor Node : %d\n",i+1);        
        printf("\n%5d. nodes cost is: ",i+1);
        scanf("%d",&costs);

        printf("    Number of neighbours = ",i+1);
        scanf("%d",&k);

        for(j=0; j<k; j++){
            printf("    Enter the %d. neighbours of %d : ",j+1,i+1);
            scanf("%d",&val);
            new_node = (struct node*)malloc(sizeof(struct node));
            new_node->data = val;
            new_node->next = NULL;
            (*new_node).cost = costs;  //////////////////////////////////////

            if(ad[i]== NULL)
                ad[i] = new_node;
            else
                last->next = new_node;
            last =  new_node;           

        }

    }
}

void print_graph(struct node *ad[],int no_of_nodes){

    struct node *ptr = NULL;
    int i,j;
    for(i=0; i<no_of_nodes; i++){
        ptr = ad[i];
        printf("\n Number %d node cost is = %d and its neighbours are :  ",i+1,*(ptr->cost));
        while(ptr != NULL){
            printf("%d\t ",ptr->data,*(ptr->cost));
            ptr = ptr->next;
        }
    }
}

最后,谁能指导我如何遵循总成本的路径并比较它们以找出哪一个是最低的?

【问题讨论】:

  • 你的 while 循环在 for 循环中做了什么?
  • 为什么你把'cost'变成一个指向int的指针,而不是简单的一个int?大概是用来存储数值的吧。
  • @Steephen 它写在里面的 for 循环中。但我不确定真相,
  • costsread_graph() 中的一个局部变量。您将指向它的指针存储在邻接图中。当read_graph() 返回时,这些指针中的每一个都是无效的,并且指向将被重用于各种其他目的的内存。这不是幸福的秘诀。到目前为止,最简单的解决方法是使您的 cost 成员成为普通的 int 变量而不是指针。如果您必须有指针(为什么?),请确保它们指向与邻接列表一样长的已分配内存,并且如果已分配,请记住释放它。
  • 另外 — scanf("%d",&amp;costs); 你有 int costs[10]; 也是一个主要错误。您正在传递一个int (*)[10],但告诉scanf() 期待一个int *;幸福不会随之而来(您每次拨打电话时都会覆盖costs 中的相同元素)。

标签: c pointers for-loop


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node{
    int data;
    int cost;
    struct node *next;
} node;

void read_graph(node *ad[], int no_of_nodes);
void print_graph(node *ad[],int no_of_nodes);

void main()
{
    int i,j,k,nodes;
    printf("\nEnter the number of nodes :");
    scanf("%d",&nodes);

    node *adj[nodes];
    memset(adj, 0, sizeof(node *) * nodes);
    read_graph(adj,nodes);
    print_graph(adj,nodes);     
}

void read_graph(node *ad[], int no_of_nodes){
    node *new_node;
    node *last;
    int i,j,k,val;  
    int cost;
    for(i=0; i< no_of_nodes; i++){
        last = NULL;

        printf("\nFor Node : %d\n",i+1);        
        printf("\n%5d. nodes cost is: ",i+1);
        scanf("%d",&cost);

        printf("    Number of neighbours = ");
        scanf("%d",&k);

        for(j=0; j<k; j++){
            printf("    Enter the %d. neighbours of %d : ",j+1,i+1);
            scanf("%d",&val);
            new_node = (node*) malloc(sizeof(node));
            new_node->data = val;
            new_node->next = NULL;
            new_node->cost = cost;

            if(ad[i]== NULL)
                ad[i] = new_node;
            else
                last->next = new_node;
            last =  new_node;           
        }
    }
}

void print_graph(node *ad[],int no_of_nodes){
    node *ptr = NULL;
    int i,j;
    for(i=0; i<no_of_nodes; i++){
        ptr = ad[i];
        printf("\n Number %d node cost is = %d and its neighbours are :  ",i+1,ptr->cost);
        while(ptr != NULL){
            printf("%d\t ",ptr->data);
            ptr = ptr->next;
        }
    }
}

代码可以很好地满足您的要求,但是我建议重新定义结构节点,例如:

typedef struct node
{
    int id;
    int cost;
    int neighbours;
} node;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多