为什么不是';我的表函数中的count变量是否递增?

为什么不是';我的表函数中的count变量是否递增?,c,C,我正在为班级做一个项目,我制作了一个有序的结构链表,其中包含两个随机的1-6卷及其总数。我正在尝试创建一个包含两列的表。左栏为数字1-13(所有可能的总数加上两个不可能的值1和13),右栏为每个可能总数的发生次数 下面是我编写的用于执行此任务的函数 void table(struct node * roll_list){ int i; int count; int total; for(i = 1; i < 14; i++){ count

我正在为班级做一个项目,我制作了一个有序的结构链表,其中包含两个随机的1-6卷及其总数。我正在尝试创建一个包含两列的表。左栏为数字1-13(所有可能的总数加上两个不可能的值1和13),右栏为每个可能总数的发生次数

下面是我编写的用于执行此任务的函数

void table(struct node * roll_list){
    int i;
    int count;
    int total;

    for(i = 1; i < 14; i++){
        count = 0;
        while(roll_list){
            total=roll_list->total;
            if(i==total){
                count++;
                roll_list = roll_list->next;
            }else{
                roll_list = roll_list->next;
            }
    }
    printf("%d : %d\n",i,count);
    }
}

因此,值都在列表和所有内容中。

您未能为每个循环初始化
roll\u list

应该使用另一个变量在循环中迭代

void table(struct node * roll_list){
    struct node * roll;
    int i;
    int count;
    int total;
    for(i = 1; i < 14; i++){
        count = 0;
        roll = roll_list;
        while(roll){
            total=roll->total;
            if(i==total){
                count++;
                roll = roll->next;
            }else{
                roll = roll->next;
            }
        }
        printf("%d : %d\n",i,count);
    }
}

您需要再次将滚动列表指向头部指针

void table(struct node * roll_list){
int i;
int count;
int total;
struct node *head = roll_list;
for(i = 1; i < 14; i++){
    count = 0;
    roll_list = head;
    while(roll_list){
        total=roll_list->total;
        if(i==total){
            count++;
            roll_list = roll_list->next;
        }else{
            roll_list = roll_list->next;
        }
}
printf("%d : %d\n",i,count);
}
}
void表(结构节点*滚动列表){
int i;
整数计数;
整数合计;
结构节点*head=滚动列表;
对于(i=1;i<14;i++){
计数=0;
滚动列表=头部;
while(滚动列表){
总计=滚动列表->总计;
如果(i==总数){
计数++;
滚动列表=滚动列表->下一步;
}否则{
滚动列表=滚动列表->下一步;
}
}
printf(“%d:%d\n”,i,计数);
}
}
void table(struct node * roll_list){
    int i;
    int count[14] = {0}; /* initialize all elements to zero */
    int total;
    while(roll_list){
        total=roll_list->total;
        if(1 <= total && total < 14){
            count[total]++;
        }
        roll_list = roll_list->next;
    }
    for (i = 1; i < 14; i++){
        printf("%d : %d\n",i,count[i]);
    }
}
void table(struct node * roll_list){
int i;
int count;
int total;
struct node *head = roll_list;
for(i = 1; i < 14; i++){
    count = 0;
    roll_list = head;
    while(roll_list){
        total=roll_list->total;
        if(i==total){
            count++;
            roll_list = roll_list->next;
        }else{
            roll_list = roll_list->next;
        }
}
printf("%d : %d\n",i,count);
}
}