【发布时间】:2018-04-24 07:49:10
【问题描述】:
我在网上找不到类似的问题,主要是因为我的问题更具体一些,并且可能是由某种逻辑错误引起的。
我正在尝试逐个阅读单词(已经完成)并制作一个链接列表(使用 typedef,已经完成)存储这些单词的所有实例及其频率。
发生的情况是我完成了代码的第一部分,即链表的初始化,但是,当它尝试链接更多节点时,程序很快就崩溃了。
当我使用输入运行代码时会发生以下情况:
C:\path-->test
hello
First word found: hello
head->word = hello
C:\path--> (Program crashes at this point)
我的代码: (我有一个头文件,但为了简单起见,我决定将所有内容都放在 .c 文件中)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct LinkedList {
char* word;
int count;
struct LinkedList *next;
};
typedef struct LinkedList *list;
int getNextWord(char* output);
list createNode();
void addEntry(list head, char *entry);
void printList(list head);
void insert(char* dest, char* toAdd);
int compare(char* str1, char* str2);
int main(){
int length;
char line[5000];
list head = NULL;
while(length = getNextWord(line) > 0){
printf("First word found: %s\n", line);
addEntry(head, line);
}
}
void addEntry(list head, char* entry){
list temp, p;
temp = createNode();
if(head == NULL){
head = temp;
insert(head->word, entry);
printf("head->word = %s", head->word);
p->next->word = NULL;
return;
}
else{
p = head;
printf("Inside else in addEntry");
while(p->next != NULL){
if(compare(p->word, entry) == 0){
p->count = p->count + 1;
return;
}
if(compare(p->word, entry) != 0 && p->word != NULL){
p = p->next;
}
if(compare(p->word, entry) != 0 && p->word == NULL){
insert(p->word, entry);
p->count = p->count + 1;
return;
}
}
p->next = temp;
}
return;
}
/* Helper function that creates a node of type (list) */
list createNode(){
list temp;
temp = (list)malloc(sizeof(struct LinkedList));
temp->next = NULL;
return temp;
}
/*list addEntry(list head, char *entry){
list temp, p;
temp = createNode();
if(head == NULL){
printf("in addEntry head == NULL\n");
head = temp;
insert(head->word, entry);
}
else {
printf("in first else case in addEntry\n");
p = head;
if((compare(entry, p->word)) == 0 && p->next != NULL){
p->count = p->count + 1;
while(0){
p = p->next;
if(p->next == NULL){
p->next = temp;
return;
break;
}
}
}
else if((compare(entry, p->word) != 0 && p->next != NULL)){
printf("in second else case in addEntry");
p = p->next;
addEntry(p, entry);
}
else if(p->word == NULL){
insert(p->word, entry);
p->count = p->count + 1;
p->next = temp;
}
else if((compare(entry, p->word) != 0 && p->next == NULL)){
p->next = temp;
insert(p->word, entry);
p->count = p->count + 1;
p->next = NULL;
}
}
return head;
}*/
void printList(list head){
list traversal = head;
while(traversal->next != NULL){
printf("%s", traversal->word);
traversal = traversal->next;
}
}
void insert(char* dest, char* toAdd){
strcpy(dest, toAdd);
}
int compare(char* str1, char* str2){
int value = strcmp(str1, str2);
return value;
}
/* I wrote this function to collect words to use */
int getNextWord(char* output){
int c;
int i = 0;
while((c = getchar()) != EOF){
if (c == ' ' || c == '\n' || c == ',')
break;
else{
output[i++] = c;
}
}
output[i] = '\0';
return i;
}
【问题讨论】:
-
现在是learn how to debug your programs 的最佳时机。不仅可以捕获和定位崩溃,还可以帮助您单步执行代码以实际查看它在做什么。
-
欢迎来到 SO。这是开始学习如何使用调试器的好时机。你可以在 GDB 中运行你的程序,看看你的崩溃发生在哪里。
-
另一个一般提示:不要一次做太多事情。迭代地创建您的程序,一次添加一个小功能,并在其间进行大量测试,以确保它在开始下一件事情之前可以正常工作。
-
你知道C中的参数是按值传递的吗?你永远不会在
main函数中改变head的值。 -
顺便说一下,最好不要在
length函数中使用length变量,因为它在循环中是不正确的。