尝试从排序列表创建新链接列表时出现分段错误11

时间:2013-02-16 00:14:43

标签: c arrays linked-list segmentation-fault nodes

为了解释发生了什么,我正在将一个已排序的链表输入到一个方法中。在此链接列表中是包含字符串的节点。节点包含字符串,以及稍后将提到的2个计数器。这些字符串是字母数字,可以重复。最后,我想输出一个链表,其中每个节点包含一个唯一的单词(全部是小写的),一个出现次数的计数器,以及一个用于该单词的不同方差的计数器(方差是由某些字符的不同大写引起的)。我相信它在逻辑上有效;但是,我遇到了分段错误。

提前致谢!

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

typedef int bool;
#define TRUE 1
#define FALSE 0


struct Node{
  char value[100];
  int numOccur;
  int numVariance;
  struct Node *next;
};




struct Node * getFinalNodes(struct Node *head){
    struct Node * curr;
    struct Node * finalNode;
    struct Node * ptr;
    struct Node * finalCurr;
    struct Node * prev;
    int m = 0;
    int z = 0;
    char lowercase1[100];
    char lowercase2[100];

    curr = head;                                        //curr = head of sorted node
    finalNode = head;
    ptr = finalNode;
    finalCurr = finalNode;

    for(m = 0; curr->value[m] != '\0'; m++){
        finalNode->value[m] = tolower(curr->value[m]);          //gets first word all lower-cased
    }

    ptr->numOccur=1;                                     //occurance is 1
    ptr->numVariance=1;                                  //variance is 1
    while(curr != NULL && curr->next != NULL){
        prev = curr;
        curr = curr->next;
        if((strcmp(prev->value, curr->value) != 0) && (strlen(prev->value) == strlen(curr->value))){            //example: Ab vs. AB
            for(z=0; curr->value[z] != '\0'; z++){                                                               //turn prev and curr into lowercase
                lowercase1[z] = tolower(prev->value[z]);                                                        //ex. Ab->ab and AB->ab
                lowercase2[z] = tolower(curr->value[z]);
            }
            if(strcmp(lowercase1,lowercase2) == 0){                                                              //if values are same, that means they are the same words, just different capitalizations
                ptr->numOccur++;
                ptr->numVariance++;
            }
            else{                                                                                               //if values are different, then they are completely different strings and put curr.value into a new finalNode
                strcpy(finalNode->value,lowercase2);
                ptr->next = finalNode;
                ptr = ptr->next;
                ptr->numOccur = 1;
                ptr->numVariance =1;
            }
        }
        else if((strcmp(prev->value, curr->value) != 0) && (strlen(curr->value) != strlen(prev->value))){                                                                     //created arrays for prev and curr
            for(z=0; curr->value[z] != '\0'; z++){                                                               //turn prev and curr into lowercase                                                      //ex. Ab->ab and AB->ab
                lowercase2[z] = tolower(curr->value[z]);
            }
            strcpy(finalNode->value,lowercase2);
            ptr->next = finalNode;
            ptr = ptr->next;
            ptr->numOccur = 1;
            ptr->numVariance =1;
       }
       else if(strcmp(prev->value, curr->value) == 0){
            ptr->numOccur++;
       }
    }
};

1 个答案:

答案 0 :(得分:0)

在循环中

while(curr != NULL){
    prev = curr;
    curr = curr->next;
    if((strcmp(prev->value, curr->value) != 0) && (strlen(prev->value) == strlen(curr->value))){

currNULL后的最后curr = curr->next;

然后strcmp(prev->value, curr->value)取消引用空指针。

您可以将循环条件更改为

while(curr != NULL && curr->next != NULL)