C语言 双向链表正确打印所有内容,但在打印功能结束时收到分割错误



好的,所以我几乎完成了我最终项目的这个程序,并且我收到了分割错误......该程序将正确执行所有操作,并将所有内容打印到屏幕上,但是它不会退出printWordLength()功能。最后打印分段错误,我确定这是一个简单的修复,但我的大脑此时此刻崩溃了。(向下滚动到最底部以获取罪魁祸首打印功能。

如果您只想使用我的代码,请随意。

目的:该程序包含一个双向链表,它将读取作为命令行参数输入的文件,从文件中读取每一行,从行中标记每个单词,并且对于每个单词,将根据其长度将其放入单词长度结构中,然后将其放入依赖于单词字符串的word_count结构中,并计算每个单词在文件中的出现次数。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DELIM " ,.+-=!?:;t"
#define MAXLINE 25000
typedef struct word_count
{
    char * word;
    int count;
    struct word_count *next;
    struct word_count *prev;
} WORD;
typedef struct word_length_count
{
    int length;
    int count;
    WORD * words;
    struct word_length_count *next;
    struct word_length_count *prev;
} WLENGTH;
int splitIntoWords(char line[]);
void processLength(char * word);
void processWord(char * word, WORD * wordCount);
void printWordLength();
WLENGTH * createWordLength(char *word);
WORD * createWordCount(char *word);
WLENGTH * wordLength = NULL;
int main(unsigned int argc, unsigned char *argv[]){
    FILE *fpin;
    char line[MAXLINE];
    int totalWordCount = 0;
    if((fpin = fopen(argv[1], "r")) == NULL)
    {
            printf("Can't open input file.n");
            exit(-1);
    }
    printf("This is the words all tokenized from the input!n");
    while(fgets(line, MAXLINE, fpin) != NULL)
    {
            line[strcspn(line, "n")] = '';
            if(line[0] == '')
                    continue;
            totalWordCount += splitIntoWords(line);
    }
    printf("Total number of words is: %dn", totalWordCount);
    printWordLength();
    printf("nFINISHED!");
}
int splitIntoWords(char line[])
{
    char *word;
    int count=0;
    word = strtok(line, DELIM);
    for(;word != NULL;)
    {
            count++;
            printf("%sn", word);
            processLength(word);
            word = strtok(NULL, DELIM);
    }
    return count;
}
void processLength(char * word)
{
    WLENGTH *wLCounter = NULL;
    WLENGTH *wLLast = NULL;
    if(wordLength == NULL)
    {
            wordLength = createWordLength(word);
            return;
    }
    wLCounter = wordLength;
    while(wLCounter != NULL)
    {
            if(strlen(word) == wLCounter->length)
            {
                    ++wLCounter->count;
                    processWord(word, wLCounter->words);
                    return;
            }
            wLLast = wLCounter;
            wLCounter = wLCounter->next;
    }
    wLLast->next = createWordLength(word);
}
void processWord(char * word, WORD * wordCount){
    WORD * wCounter = NULL;
    WORD * wLast = NULL;
    if(wordCount == NULL)
    {
            wordCount = createWordCount(word);
            return;
    }
    wCounter = wordCount;
    while(wCounter != NULL)
    {
            if(strcmp(word, wCounter->word) == 0)
            {
                    ++wCounter->count;
                    return;
            }
            wLast = wCounter;
            wCounter = wCounter->next;
    }
    wLast->next = createWordCount(word);
}
WLENGTH * createWordLength(char *word)
{
    WLENGTH *wLCounter = NULL;
    wLCounter = (WLENGTH*)malloc(sizeof(WLENGTH));
    //wLCounter->count = (int*)malloc(int));
    //wLCounter->length = (int*)malloc(int));
    wLCounter->words = createWordCount(word);
    wLCounter->count = 1;
    wLCounter->length = strlen(word);
    wLCounter->next = NULL;
    return wLCounter;
}
WORD * createWordCount(char *word)
{
    WORD *wCount = NULL;
    wCount = (WORD*)malloc(sizeof(WORD));
    wCount->word = (char*)malloc(strlen(word+1));
    strcpy(wCount->word, word);
    wCount->count = 1;
    wCount->next = NULL;
    return wCount;
}
void printWordLength(){
    WLENGTH * temp = wordLength;
    WORD * tempWORD = wordLength->words;
    while(temp != NULL)
    {
            tempWORD = temp->words;
            printf("nFor Word Length: %d : There are: %d occurances!n",  temp->length, temp->count);
            while(tempWORD != NULL)
            {
                    printf("t%stoccurs:%dn", tempWORD->word, tempWORD->count);
                    tempWORD = tempWORD->next;
            }
            temp = temp->next;
    }
}

在我为 tempWORD 添加 while 循环之前,我没有收到任何分段错误。但是我脑子放屁的时刻是我不知道这个问题。也许指针问题?

在没有努力理解代码的情况下,最后一行看起来像一个问题 - 您需要在分配 tempWORD = temp->words 之前检查 temp != NULL。除非您打算在移动到临时>下一个之前执行 tempWord = 临时>单词分配

相关内容

  • 没有找到相关文章

最新更新