【发布时间】:2014-12-31 13:42:31
【问题描述】:
我已经用不同的列表测试了 addNode 函数和 printf 循环,它们工作正常。但是这个有问题,因为当我打印列表时它会打印头部,然后所有其他单词都与用户给出的最后一个单词相同。
这是addnode(这个词是用户给出的-我检查过,它工作正常)
struct list* addNode(struct list* head, char *word){
struct list *curr,*help,*Nhead;
curr=(struct list *)malloc(sizeof(struct list));
curr->sorted=head->sorted;
if(curr->sorted==false){
Nhead=head;
while(head->next!=NULL){
head=head->next;
}
curr->data.word=word;
curr->prev=head;
curr->next=NULL;
head->next=curr;
}
else{
Nhead=head;
for(help=head; help!=NULL; help=help->next){
if(strcmp(word,help->data.word)<0){
break;
}
}
if(help==NULL){
for(help=head; help->next!=NULL; help=help->next){}
curr->next=NULL;
curr->data.word=word;
curr->prev=help;
help->next=curr;
}
else{
curr->next=help;
curr->prev=help->prev;
curr->data.word=word;
help->prev=curr;
if(help!=head){
help=curr->prev;
help->next=curr;
}
else{
Nhead=curr;
}
}
}
return Nhead;
}
这就是我的打印方式
for(curr=pathWordsH; curr!=NULL; curr=curr->next){
printf("%s",curr->data.word);
if(curr->next!=NULL){
printf("-->");
}
}
【问题讨论】: