2015-10-14 59 views
-1

我试图创建一个程序,通过解析一些字符串并将它们添加到链接列表中,然后打印出每个字符串的出现开始。链接列表与字符串

#include<stdio.h> 
#include<stdlib.h> 
#include<stdbool.h> 
typedef struct Node Node; 

struct Node 
{ 
    char* word; 
    int count; 
    struct Node *next; 
}; 

Node *head = NULL; 
Node *curr = NULL; 

Node* listAdd(char* word, bool toEnd) { 
    Node* tmp = head; 
    while (tmp) { 
     if (strcmp(tmp, word) == 0) { 
      tmp->count++; 
      return tmp; 
     } 
     tmp = tmp->next; 
    } 
    printf("allocate memory for node"); 
    Node *ptr = malloc(sizeof(Node)); 

    printf("initialize count to 0"); 
    ptr->count = 0; 

    printf("allocate memory to hold word"); 
    ptr->word = malloc(strlen(word) + 1); 

    printf("copy the current word"); 
    strcpy(ptr->word, word); 

    ptr->next = NULL; 

    if (toEnd) 
    { 
     curr->next = ptr; 
     curr = ptr; 
    } 
    else 
    { 
     ptr->next = head; 
     head = ptr; 
    } 
    return ptr; 
} 

void printList() 
{ 
    Node *ptr = head; 
    while (ptr) 
    { 
     printf("\nThe word [%s] has had [%d] occurrences.\n",ptr->word, ptr->count); 
     ptr = ptr->next; 
    } 
} 

char* readWord() 
{ 
    static char buffer[100]; 
    scanf("%s", buffer); 
    printf("listAdd() call"); 
    listAdd(buffer); 
    return buffer; 
} 

int main(void) 
{ 
    int i = 0; 
    printf("How many words would you like to type?\n"); 
    scanf("%d", &i); 
    for (i; i != 0; i--) 
    { 
     readWord(); 
    } 
    printList(); 
} 

电流输出:

How many words would you like to input? 
3 
hi 
bye 
yes 
How many words would you like to input? 
Occurrences: 12086064 

任何援助将不胜感激 - 我仍然位于C新手从C#:(

+0

建议你坚持和调试这个自己,而不是转向SO立竿见影。特别是,启动您最喜欢的调试器,逐步执行您的程序,并在每个阶段检查代码是否按预期运行。甚至一些可靠的调试打印语句将会帮助您理解您的程序实际上在做什么。 – kaylum

+1

你不使用函数'searchList'。 'count'没有被初始化.. –

+0

您还需要分配空间来保存'listAdd()'中的实际字符串,例如'ptr-> word = malloc(strlen(word)+ 1);'然后复制字符串'strcpy(ptr-> word,word);' – Cyclonecode

回答

1

你的代码包含一些错误。你两次分配好自己的Node PTR:

Node* listAdd(char* word, bool toEnd) { 
    // .. 

    Node *ptr = malloc(sizeof(Node)); 
    return ptr; 
} 

return语句之前删除的最后一个。

您还需要分配内存来保存每个读取的单词。现在,您每次拨打readWord()时都会覆盖缓冲区。那么你会做这样的事情:

Node* listAdd(char* word, bool toEnd) { 
    // allocate memory for node 
    Node *ptr = malloc(sizeof(Node)); 
    // initialize count to 0 
    ptr->count = 0; 
    // allocate memory to hold word 
    ptr->word = malloc(strlen(word) + 1); 
    // copy the current word 
    strcpy(ptr->word, word); 

printList()功能需要是这个样子:

void printList() { 
    Node* ptr = head; 
    while(ptr) { 
    printf("Word: %s %d\n", ptr->word, ptr->count); 
    ptr = ptr->next; 
    } 
} 

因为你从来没有检查输入的单词列表中已经存在的每个单词总是会被报告为有1次发生。这可能是固定这样的:

// check if the word alreay exists in the list and then increment its count by 1 
// this code should go at the top (before allocating ptr) in listAdd() 
Node* tmp = head; 
while(tmp) { 
    if(strcmp(tmp->word, word) == 0) { 
    tmp->count++; 
    return tmp; 
    } 
    tmp = tmp->next; 
} 

当然,你也应该退出应用程序之前释放所分配的内存:

void freeList() { 
    Node* ptr = head; 
    Node* tmp = 0; 
    while(ptr) { 
    tmp = ptr->next; 
    free(ptr->word); 
    free(ptr); 
    ptr = tmp; 
    } 
+0

我按照你的建议更新了OP,但我仍然得到相同的结果。是因为我没有初始化'count'? – Sleepless

+0

@Sleepless - 是的,这可能是问题所在。看看上面的代码,我正在初始化数到0. – Cyclonecode

+0

是的,我看到你的编辑,并在我的机器上尝试它(并编辑OP相当晚!),但仍然没有运气。同样,当我们注意到这个时,我应该在我的'if(head == NULL)'块中初始化'count'为0,然后在块之后继续增加它? – Sleepless

0

未来您需要使用malloc和strcpy存储字和strcmp来比较它们,你正在做的是保存重复引用缓冲区数组,你正在读这些单词到