[英]program crashes while iterating through a linked list
我應該創建一個具有以下屬性的鏈表:
struct node{
char *word;
int times;
struct node *next;
}head;
我必須從文件中讀取每個單詞,檢查它是否存在於列表中,如果不存在,我應該添加它。 如果已經存在,我需要將times
增加times
。 我使用以下代碼來完成該任務:
void append(char *wrd, struct node *start){
struct node *current = start;
while(current->next != NULL){
current = current->next;
}
current->next = malloc(sizeof(struct node));
strcpy(current->word, wrd);
current->times = 1;
current->next->next = NULL;
}
int iterate(char *wrd, struct node *start){
struct node *current = start;
while(current != NULL){
if(strcmp(current->word, wrd)==0){
current->times++;
return 1;
}
current = current->next;
}
return 0;
}
void read_file(struct node *start){
FILE *fp;
char wrd[20];
puts("give file name and format(max 19 characters)");
gets(wrd);
fp = fopen((const char*)wrd, "r");
fscanf(fp, "%s", wrd);
start->word = malloc(strlen(wrd)+1);
strcpy(start->word, wrd);
while(fscanf(fp, "%s", wrd) != EOF){
if(!iterate(wrd, start)){
append(wrd, start);
}
}
}
void print_data(struct node *start){
struct node *current = start;
while(current->next != NULL){
printf("word: %s , times: %d", current->word, current->times);
}
}
int main(int argc, char *argv[]) {
struct node *start = &head;
read_file(start);
return 0;
}
append
一個單詞,創建一個包含該單詞的新節點,然后將該節點添加到列表中。
iterate
一個單詞並在列表中搜索匹配項。 如果列表中已經存在該單詞,則times
加一。 如果未找到匹配項,則返回0
,反之則返回1
。
read_file
初始化頭節點,讀取文件,並為其讀取的每個單詞調用上述函數。
假設我有一個包含以下單詞的文本文件:
hello hey world hello
world this is supposed to work
but it does not
程序成功運行了前三個單詞並創建了節點。 找到匹配hello
,程序崩潰。 我確定錯誤在於iterate
,但我不知道是什么原因造成的。 任何幫助是極大的贊賞。
首先,我不明白為什么您可以自信地說您的程序在iterate
函數中存在錯誤。
您尚未在strcpy()
之前為word
指針分配任何內存。 我建議在您的append()
函數中這樣做:
void append(char *wrd, struct node *start){
struct node *current = start;
if(current == NULL){
start = (struct node*)malloc(sizeof(struct node));
start->word = (char *)malloc((strlen(wrd) + 1)*sizeof(char));
strcpy(start->word, wrd);
start->times = 1;
start->next = NULL;
return;
}
while(current->next != NULL){
current = current->next;
}
current->next = (struct node*)malloc(sizeof(struct node));
current->next->word = (char*)malloc((strlen(wrd) + 1)*sizeof(char)); //add this line
strcpy(current->next->word, wrd); //not current->word
current->next->times = 1; //not current->times
current->next->next = NULL;
}
注意這里,在append函數中,您沒有檢查列表是否為空。 if
塊將執行相同的操作。 還要注意您在使用strcpy()
時犯的錯誤。 您想將新單詞復制到新指針中,但正在新節點的父代指針中進行。
現在,您的read_file()
函數看起來要簡單得多!
void read_file(struct node *start){
FILE *fp;
char wrd[20];
puts("give file name and format(max 19 characters)");
gets(wrd);
fp = fopen((const char*)wrd, "r");
while(fscanf(fp, "%s", wrd) != EOF){
if(!iterate(wrd, start)){
append(wrd, start);
}
}
}
我不認為您的iterate
函數需要更新,但是print_data()
當然可以:
void print_data(struct node *start){
struct node *current = start;
while(current != NULL){
printf("word: %s , times: %d", current->word, current->times);
current = current->next;
}
}
似乎唯一沒有錯誤的函數是iterate()
。 玩指針時發生! :P
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.