【发布时间】:2017-05-29 09:16:00
【问题描述】:
我正在尝试在 C 中实现一个简单的链表,但似乎没有正确添加第一个元素。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
typedef struct person{
int phoneNumber;
char* name;
struct person *nextPers;
}person;
person* firstPerson=NULL;
person* lastPerson=NULL;
void addPerson (person* _person){
/*Adds a new person to the linked list after the last one added
*/
if (firstPerson!=NULL){
fprintf(stderr,"DEBUG(addPerson): First person is %s \n",firstPerson->name);
fprintf(stderr,"DEBUG (addPerson): Last Person is (before adding the new one) %s \n",lastPerson->name);}
fprintf(stderr,"DEBUG: Adding person %s \n",_person->name);
_person->nextPers= NULL;
if (firstPerson==NULL){
firstPerson = _person;
lastPerson= _person;
fprintf(stderr,"DEBUG: The head of the list is %s \n",firstPerson->name);
}else{
fprintf(stderr,"DEBUG: Last person (before adding the new one) %s \n",lastPerson->name);
fprintf(stderr,"DEBUG (addPerson):Adding to the list %s \n",_person->name);
lastPerson->nextPers =_person;
lastPerson=_person;
fprintf(stderr,"DEBUG: Last person %s \n",lastPerson->name);
}
}
int main(int argc, char* argv[]){
char line[80],name[80];
int number;
setvbuf(stdout,(char*)malloc(sizeof(char)*80),_IOLBF,80);
setvbuf(stdin,(char*)malloc(sizeof(char)*80),_IOLBF,80);
for(;fgets(line,80,stdin);){
if(!strcmp(line,"Finish\n"))
break;
sscanf(line,"%[^:]: %d",name,&number);
/* Stores the person introduced from stdin as Name:phone */
if(firstPerson!=NULL){
fprintf(stderr,"DEBUG (Before storing new data aparently): First person is %s \n",firstPerson>name);
}
person * newPerson= malloc(sizeof(person));
newPerson->phoneNumber = number;
newPerson->name = name;
fprintf(stderr,"DEBUG: Adding person %s \n",newPerson->name);
fprintf(stderr,"DEBUG: phone number %d \n",newPerson->phoneNumber);
addPerson(newPerson);
fprintf(stderr,"DEBUG: There is a new person on the list\n");
}
}
预期的输出是:
Harvey:12345
DEBUG: Adding person Harvey
DEBUG: phone number 12345
DEBUG: Adding person Harvey
DEBUG: The head of the list is Harvey
DEBUG: There is a new person on the list
Adam:23456
DEBUG (Before storing new data aparently): First person is Harvey
DEBUG: Adding person Adam
DEBUG: phone number 23456
DEBUG(addPerson): First person is Harvey
DEBUG (addPerson): Last Person is (before adding the new one) Harvey
DEBUG: Adding person Adam
DEBUG: Last person (before adding the new one) Harvey
DEBUG (addPerson):Adding to the list Adam
DEBUG: Last person Adam
DEBUG: There is a new person on the list
但是,输出却是:
Harvey:12345
DEBUG: Adding person Harvey
DEBUG: phone number 12345
DEBUG: Adding person Harvey
DEBUG: The head of the list is Harvey
DEBUG: There is a new person on the list
Adam:23456
DEBUG (Before storing new data aparently): First person is (null) //Wrong, its Harvey
DEBUG: Adding person Adam
DEBUG: phone number 23456
DEBUG(addPerson): First person is Adam //Wrong, its Harvey
DEBUG (addPerson): Last Person is (before adding the new one) Adam //Wrong, its Harvey
DEBUG: Adding person Adam
DEBUG: Last person (before adding the new one) Adam //Wrong its Harvey
DEBUG (addPerson):Adding to the list Adam
DEBUG: Last person Adam
DEBUG: There is a new person on the list
总结一下,主要思路是得到一个链表就是:Harvey->Adam->(next) 但是不知道哪里出错了。
【问题讨论】:
-
fprintf(stderr, "DEBUG (Before storing new data aparently): First person is %s \n", firstPerson>name);->fprintf(stderr, "DEBUG (Before storing new data aparently): First person is %s \n", firstPerson->name); -
并注意编译器警告:
lastPerson->nextPers = _person;:这里_person的类型是person*,而lastPerson->nextPers的类型是struct proceso *。你搞混了。 -
您向我们展示了预期的和实际的输出,这很好,但您也应该展示输入。
-
最后但同样重要的是:学习如何使用调试器,而不是进行“printf 调试”。它的效率要高得多,而且您花在学习上的时间会很快得到回报。
标签: c linked-list head