0
所以我相当新的C和不得不使用链接列表的数据库,我已经尝试过,无法找到问题。当添加一个节点时,它会将其打印出来,但是当添加更多节点时,它仅在打印出最近添加的节点时循环,并且从不打印出最后一个节点(首先输入的节点)。链接列表循环输出在C
struct person
{
char sex;
int age;
struct person *prev, *next;
};
struct person* first = NULL;
static void add_person(void)
{
struct person* temp = (struct person*)malloc(sizeof(struct person));
if (temp == NULL)
printf("Unable to allocate memory");
{
printf("Enter the gender: ");
scanf("%c", person->sex);
printf("Enter the age: ");
scanf("%d", person->age);
if(first==NULL){
temp->prev=NULL;
temp->next=NULL;
first=temp;
}
else{
temp->prev=NULL;
temp->next=first;
first->prev=temp;
first=temp;
}
free(temp);
}
static void print(void)
{
struct person* present = first;
while(present != NULL){
printf("%c", present->sex);
printf("%i", present->age);
present=present->next;
}
凡'start'变量声明? – oklas
也许'开始'应该是'第一'?为什么在返回之前释放'temp'? –
对不起,开始是第一,现在改变它。我应该不是在那个地方解放临时工吗? – birdleaf