链表不断出现段错误

时间:2018-12-04 17:59:06

标签: linked-list

我是新来的人,在创建链表时遇到问题。我一直遇到细分错误,不确定问题出在哪里。当我尝试输入函数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);

0 个答案:

没有答案