首页 > 解决方案 > 链表不断出现段错误

问题描述

我是新来的,在制作链接列表时遇到问题。我不断收到分段错误,不确定问题出在哪里。当我尝试输入函数 scanzip 时,它一直在发生。我只包含了停止工作的代码。在 Visual Studio 中运行时,它会在带有 while 循环的 scapzip 中的行上给出访问冲突。谢谢

谢谢

  typedef struct houseType house;
  struct houseType {
     unsigned long int id;
     int value;
     int bedrooms;
     float bathrooms;
     int sqft;
     int yr_built;
     int zipcode;
     house * nextzip; // One pointer for list of ordered zipcodes and one         for ordered years
     house * nextyr;
  };
  house * scanzip(house * head, int zip) { //Finds out where to put           node based on zipcode
     house * prev = NULL, * cur = NULL;
     prev = head;
     cur = head - > nextzip;
     while ((cur != NULL) && (cur - > zipcode < zip)) {
        prev = cur;
        cur = cur - > nextzip;
     }
     return prev;
  }

  house * scanyr(house * head, int yr) { // Finds out where to put     node based on yr_built
     house * previous = NULL, * current = NULL;
     previous = head;
     current = head - > nextyr;
     while ((current != NULL) && (current - > yr_built < yr)) {
        previous = current;
        current = current - > nextyr;
     }
     return previous;
  }

  int main() {
        house * previous = NULL, * current = NULL, * head = NULL, * newnode = NULL, * zip = NULL, * prezip = NULL, * yr = NULL, * preyr = NULL;
        head = (house * ) malloc(sizeof(house));
        previous = head;

        current = head - > nextzip;
        float aveValue = 0;
        float aveBed = 0;
        float aveSize = 0;
        int max = previous - > value;
        int min = previous - > value;
        int yrMax, yrMin;
        int counter = 1;
        int maxZip = previous - > zipcode, minZip = previous - > zipcode;

        int tempbed, tempyr, tempzip, tempvalue, tempsqft;
        unsigned long int tempid;
        float tempbath;
        char a[1000];

        FILE * data = NULL;
        fopen_s( & data, "c:/Users/joel/Downloads/house-info-v4.txt", "r");

        if (fgets(a, 100, data) != "\n") // Gets rid of first line of file
           puts(a);

        fscanf_s(data, "%lu,%d,%d,%f,%d,%d,%d", & tempid, & tempvalue, & tempbed, & tempbath, & tempsqft, & tempyr, & tempzip);
        head - > id = tempid;
        head - > value = tempvalue;
        head - > bedrooms = tempbed;
        head - > bathrooms = tempbath;
        head - > sqft = tempsqft;
        head - > yr_built = tempyr;
        head - > zipcode = tempzip;
        printf("%lu,%d,%d,%f,%d,%d,%d\n", head - > id, head - > value, head - > bedrooms, head - > bathrooms, head - > sqft, head - > yr_built, head - > zipcode);

        while (fscanf_s(data, "%lu,%d,%d,%f,%d,%d,%d", & tempid, & tempvalue, & tempbed, & tempbath, & tempsqft, & tempyr, & tempzip)) {
           printf("%lu,%d,%d,%f,%d,%d,%d", tempid, tempvalue, tempbed, tempbath, tempsqft, tempyr, tempzip);
           newnode = (house * ) malloc(sizeof(house)); //Allocates memory to a new node
           newnode - > id = tempid;
           newnode - > value = tempvalue;
           newnode - > bedrooms = tempbed;
           newnode - > bathrooms = tempbath;
           newnode - > sqft = tempsqft;
           newnode - > yr_built = tempyr;
           newnode - > zipcode = tempzip;
           newnode - > nextzip = NULL;
           newnode - > nextyr = NULL;
           printf("yes");

           zip = scanzip(head, newnode - > zipcode);

标签: linked-list

解决方案


推荐阅读