【问题标题】:Issue with creating head of a linked list创建链表头的问题
【发布时间】:2018-10-08 04:52:44
【问题描述】:

我的代码旨在从哈希链表中读取一系列字符串,将它们全部转换为小写,将它们放入数组中以对其进行快速排序,然后将它们放入称为字数的数据结构中,其中包括单词以及它在文档中出现的次数。目前,当我运行代码时,它会使用我正在使用的打印语句正确打印出来,但是当我打印出来时,head 总是设置为 null。

这里是 wordCount 声明:

typedef struct wordCount
{
    int count;
    char *word;
    struct wordCount* next;
} wordCount;

这是应该执行我上面描述的方法段。

else
    {
        char *toSort[curSize];
        int linkedListTraverse = 0; //Array index for each linked list node
        while(linkedList != NULL)
        {
            toSort[linkedListTraverse] = (char*) malloc(sizeof(linkedList->string));
            strcpy(toSort[linkedListTraverse],linkedList->string); //Copy the data from the linked list into an array 
            linkedList = linkedList->next;
            linkedListTraverse++;
        }
        int i = 0;
        while(i < curSize) //Convert all of the words to lowercase
        {
            char* str = toSort[i];
            char *p;
            for (p = str; *p != '\0'; p++)
                *p = (char)tolower(*p);
            i++;
        }
        i = 0;
        qsort(toSort, curSize, sizeof(char*), stringCmpFunc); //Sort the current node
        while(i < curSize)
        {
            printf("%s\n", toSort[i]);
            i++;
        }
        int curWordIndex = 0;
        int checkWordIndex = 1;
        wordCount *wordHead = NULL;
        wordCount *curWord = wordHead;
        while(curWordIndex < curSize)
        {
            curWord = (wordCount*) malloc(sizeof(wordCount));
            curWord->word = toSort[curWordIndex]; //Set the word
            curWord->count = 1; //Start the count out at 1
            while(strcmp(toSort[curWordIndex], toSort[checkWordIndex]) == 0) //While the two words are equal
            {
                checkWordIndex++; //Advance the leading index check
                curWord->count++;
                if(checkWordIndex >= curSize) //If the leading index goes beyond the array bounds
                    break;
            }
            if(checkWordIndex < curSize)
            {
                curWordIndex = checkWordIndex;
                checkWordIndex = curWordIndex + 1;
            }
            if(checkWordIndex >= curSize) //If the leading index goes beyond the array bounds
            {
                    if(strcmp(curWord->word, toSort[curWordIndex]) != 0)
                    {
                        printf("%s %d\n", curWord->word, curWord->count);
                        curWord = curWord->next;
                        curWord = (wordCount*) malloc(sizeof(wordCount));
                        curWord->word = toSort[curWordIndex]; //Set the word
                        curWord->count = 1; //Start the count out at 1
                    }
                    break;
            }
            //printf("CurWordIndex: %d\n CheckWordIndex: %d\n",curWordIndex, checkWordIndex);
            printf("%s %d\n", curWord->word, curWord->count);
            curWord = curWord->next; //Advance to the next node in the linked list
        }
        printf("%s %d\n", curWord->word, curWord->count);

这里是只打印空的代码段

curWord = wordHead;
        while(curWord != NULL)
        {
            printf("%s %d\n", curWord->word, curWord->count);
            curWord = curWord->next;
        }

【问题讨论】:

    标签: c linked-list malloc


    【解决方案1】:

    if (wordHead == NULL) { wordHead = curWord; }
    

    之后

    curWord = (wordCount*) malloc(sizeof(wordCount));
    

    更新

    这是另一个问题:

    curWord = curWord->next;
    curWord = (wordCount*) malloc(sizeof(wordCount));
    

    应该是:

    curWord->next = (wordCount*) malloc(sizeof(wordCount));
    curWord = curWord->next;
    

    注意: 请关注rules,它将帮助我们为您提供帮助。

    更新/2

    替换这个:

    while(curWordIndex < curSize) {
        curWord = (wordCount*) malloc(sizeof(wordCount));
    

    用这个:

    while(curWordIndex < curSize) {
        wordCount* tmp = (wordCount*) malloc(sizeof(wordCount));
        if (curWord) { curWord->next = tmp; }
        curWord =  tmp;
    

    【讨论】:

    • 这使得头部设置正确,但我仍然认为链表的其余部分设置不正确。每当从头开始打印链表的循环运行时,只打印链表的头。
    • 您好,再次感谢您的帮助。即使进行了您建议的两个更改,使用头部打印出链表的循环仍然只打印链表的头部然后退出。另外,我忽略了哪一部分规则?我想确保我不违反规则。
    • @ColinNull,至少它不完整。
    • 您好,很抱歉很难需要这么多帮助,但是您添加的循环更改并没有解决问题,它仍然只是创建链表的头部而不是将事物连接在一起正确。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多