0

只是为了解释发生了什么,我将一个排序的链表输入到一个方法中。在这个链表中是包含字符串的节点。节点包含字符串和后面会提到的 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++;
       }
    }
};
4

1 回答 1

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)
于 2013-02-16T00:26:49.953 回答