在C编程中循环使用两个变量(一个是指针)

时间:2018-06-06 00:18:09

标签: c pointers for-loop

我简单地说有关2个变量的循环问题。
一个经典的变量i=0 > to the end of bound和 第二个是*(ptr->cost)之类的,并使用指针显示值。

我有一个问题第二个实际上我不能把它放在for循环中。我从用户那里得到成本变量,我想一起打印 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;
        }
    }  
}

和出局
enter image description here

节点1应为50,但输出显示为70.

另外我认为Cost变量不包含在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;
        }
    }
}

最后,任何人都可以指导我如何遵循总和成本的路径并比较它们以找出哪一个最低?

1 个答案:

答案 0 :(得分:0)

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