我试图在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-&gt; Adam-&gt;(下一个)但我不知道错误的位置。
答案 0 :(得分:1)
你犯了一些错误:
struct proceso *nextPers;
这没有意义,改为
person *nextPers;
然后保存在结构中的名称指向name [80](main()中的var),因此每次此var更改时,结构中的名称也会更改。你必须在你的结构上复制它;
memcpy(newPerson->name, name, 80);
并将结构更改为:
typedef struct person{
int phoneNumber;
char name[80];
struct person *nextPers;
}person;
请记住,完成后还要清空列表中的节点
答案 1 :(得分:1)
链接列表部分代码是正确的,这可能会让你发疯,我知道。您的代码中还有一些其他错误,与链接列表无关。
fprintf(stderr,"DEBUG (Before storing new data aparently): First person is %s \n",firstPerson>name);
看到你写了firstPerson&gt; name而不是firstPerson-&gt; name,这是做一个布尔比较并返回零 - 这就是为什么你有一个&#34; null&#34;那里。
第二个错误在这里:
newPerson->phoneNumber = number;
newPerson->name = name
您将phoneNumber设置为名称和数字数组的指针。所以当你在这里做第二次印刷时:
DEBUG (addPerson): Last Person is (before adding the new one) Adam //Wrong, its Harvey
它实际上正在检索正确的值 - 因为你将Harvey交给了Adam。
修复很简单:将字符串复制到newPerson中,而不是将其指向名称。
newPerson->name = malloc(sizeof(char) * strlen(name) + 1);
strcpy(newPerson->name, name);
这应该可以解决您的问题。