一个单向链的问题,为什么输出不正常,谢谢!
#include <stdio.h>#include <stdlib.h>
struct students
{
char name[20];
int age;
char gender;
struct students *next;
};
struct students *create();
void printS();
int main (void)
{
struct students *head;
head=NULL;
head=create(head);
printS(head);
return 0;
}
struct students *create(struct students *head)
{
struct students *tail,*p;
int i=1;
char ext[20]="无";
tail=(struct students *)malloc(sizeof(struct students));
printf("请输入学生信息,如果姓名为 \"无\" 退出!存放地址为P_%d=add%p\n",i,tail);
printf("输入格式为了姓名\t年龄\t姓别.\n");
scanf("%s%d%c",&tail->name,&tail->age,&tail->gender);
tail->next=NULL;
while(strcmp(ext,tail->name))
{
if (head==NULL)
head=tail;
else
{
tail->next=p;
tail=p;
}
p=(struct students *)malloc(sizeof(struct students));
i=i+1;
//printf("输入格式为了姓名\t年龄\t姓别.\n");
printf("请输入学生信息,如果姓名为 \"无\" 退出!存放地址为P_%d=add%p\n",i,tail);
scanf("%s %d %c",&p->name,&p->age,&p->gender);
}
tail->next=NULL;
printf("输入学生信息结束!\n\n");
return head;
}
void printS(struct students *head)
{
struct students *temp;
temp =head;
printf("输入学生信息为:\n");
printf("姓名\t年龄\t姓别.\n");
while(temp)
{
printf("%s\t%d\t%c\n",&temp->name,&temp->age,&temp->gender);
temp=temp->next;
}
printf("打印学生信息结束!\n\n");
}