为了解释发生了什么,我将一个排序的链表输入到一个方法中。在这个链接列表中是包含字符串的节点。节点包含字符串和后面将要提到的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] != ' '; 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] != ' '; 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] != ' '; 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++;
}
}
};
在环
while(curr != NULL){
prev = curr;
curr = curr->next;
if((strcmp(prev->value, curr->value) != 0) && (strlen(prev->value) == strlen(curr->value))){
CCD_ 1是在CCD_ 3之后的末尾处的CCD_。
则strcmp(prev->value, curr->value)
取消引用空指针。
您可以将循环条件更改为
while(curr != NULL && curr->next != NULL)