c - 循环链表——无限循环

标签 c arrays list loops linked-list

自学编程后,我决定制作一个简单的瓦数计算器。每个州都有不同的(平均)每千瓦时价格。我的问题从代码中的第 34 行开始。 curr->next 永远不会为空。

我做错了什么?

#include<stdio.h>
#include<stdlib.h>
#include<string.h>


float calculate(float w, float cents, float h);

struct states 
{
    char *name;
    char *price;
    struct states *next;
};

int main()
{
    float watts,hours,cents;
    int a = 0;
    struct states *curr, dict[52];
    char st_ab[52][3] = {"ID","ND","WA","AR","UT","LA","WY","SD","NE","KY","WV","OK","OR","MT","MS","TN","IN","NC","MS","IA","KS","VA","NM","MN","SC","AZ","GA","AL","CO","OH","TX","IL","FL","NV","MI","WI","PA","MD","DC","DE","MA","RI","CA","ME","VT","NJ","NH","AK","CT","NY","HI"};
    char st_pr[52][5] = {"8.0","8.1","8.2","8.7","8.8","8.9","8.9","9.0","9.0","9.0","9.2","9.2","9.4","9.6","9.7","9.8","10.0","10.2","10.3","10.5","10.5","10.5","10.7","10.9","11.0","11.1","11.1","11.1","11.2","11.2","11.3","11.7","11.7","11.7","13.0","13.0","13.2","13.7","13.7","13.7","14.8","14.9","15.2","15.5","16.1","16.3","16.5","17.5","18.1","18.1","33.2"};
    char state[2];

    for(a=0;a<=52;a++)
    {
        memset(&dict[a],0,sizeof(struct states)); /* zero out structure */
        if(a==52)
        {
            break;  /* if this is the last struct, leave it NULL so we can loop through linked list */
        }
        dict[a].next=&dict[a+1];        
    }

    for(curr = dict;curr ->next != NULL;curr = curr->next)  /* fill our list with state abbr. and cost/KWh */
    {
        curr->name = st_ab[a];
        curr->price = st_pr[a];
        a++;
    }

    puts("Dictionary loaded!");
    printf(" Two letter state code: ");                 /* get info from user */
    scanf("%s",state);

    printf(" Wattage of appliance: ");
    scanf("%f",&watts);

    printf(" Hours of use per day: ");
    scanf("%f",&hours);

    for(curr = dict; curr->next != NULL; curr=curr->next)
    {
        printf("comparing %s with %s\n", curr->name,state);  /* search for our state, set cents respectively */
        if((strcmp(curr->name,state)) == 0)
        {
            printf("State found! -- %s\n",state);
            cents=atof(curr->price);
            printf("%f cents\n",cents);
            break;
        } else {
            printf("state didn't match\n");
            continue;
        }
    }

    printf("\nAverage cost per day: %.2f\n",  calculate(watts,cents,hours));
    printf("Average cost per year: %.2f\n\n", (calculate(watts,cents,hours)) * 365);    

    return 0;

}

float calculate(float w, float c, float h)
{   
    float kwh = (w/1000) * h;
    float cos = kwh*(c/100);
    return(cos);
}

最佳答案

问题出在

dict[a].next = &dict[a+1];

C 标准保证 &dict[a+1]永远不会为 NULL,即使它已经超出了数组的末尾。这会导致下一个循环超出列表末尾,从而导致未定义的行为(程序可能会执行任何操作)。第一个循环后,执行

dict[51].next = NULL;

结束链接列表。

编辑:与 for(a=0;a<=52;a++)实际上,您将两个元素移过数组末尾,因此该行为在两个地方未定义。要解决这个问题,您需要循环直到 a<52 .

关于c - 循环链表——无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15684730/

相关文章:

c++ - 将表示为链表的两个大数相乘

python - 仅当字符串匹配时才在 python 中连接字符串

c - 解释输出(C 中的二维数组)

c - 访问 C 库的最 pythonic 方式是什么——例如,OpenSSL?

c++ - C 引用 C++ extern

arrays - 错误 SIGABRT - 无法在我的 TableViewController 中加载多个数组

java - 如何访问对象内部的元素

python - 使用 numpy 处理点矩阵乘法组

c - C语言中如何避免缓冲区溢出

Python 按特定定义的规则对项目进行排序