更新时间2018-11-01 11:22:12
# include<stdio.h>
# include<stdlib.h>
# define LEN sizeof(struct Student)
struct Student
{
long num;
char name[8];
char sex;
int age;
struct Student *next;
};
struct Student lista,listb;
int n,sum=0;
struct Student *creat()
{
struct Student *p1,*p2,*head;
n=0;
p1=p2=(struct Student *) malloc(LEN);
printf("NO.:");
scanf("%ld",&p1->num);
printf("name:");
scanf("%s",p1->name);
printf("sex:");
scanf("%c",&p1->sex);
printf("ages:");
scanf("%d",&p1->age);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct Student *) malloc(LEN);
printf("NO.:");
scanf("%ld",&p1->num);
printf("name:");
scanf("%s",p1->name);
printf("sex:");
scanf("%c",&p1->sex);
printf("ages:");
scanf("%d",&p1->age);
}
p2->next=NULL;
return head;
}
struct Student *del(struct Student *head,int ages)
{
struct Student *p1,*p2;
int find;
p1=head;
p2=p1;
if(p1->age==ages)
{
p2=p1->next;
head=p1=p2;
find=1;
}
else p1=p1->next;
while(p1!=NULL)
{
if(p1->age==ages)
{ p2->next=p1->next;
find=1;
}
else p2=p1;
p1=p1->next;
}
if(!find)
printf(" not found %d",ages);
return head;
}
void print(struct Student *head)
{
struct Student *p;
printf(" Now,these %d records are : ",n);
p=head;
if(head!=NULL)
do
{
printf("%ld %s %c %d ",p->num,p->name,p->sex,p->age);
p=p->next;
}while(p!=NULL);
}
int main()
{
struct Student *ahead;
int ages;
printf("input list a: ");
ahead=creat();
sum=sum+n;
printf("input the age to delete:");
scanf("%d",&ages);
ahead=del(ahead,ages);
print(ahead);
return 0;
}
应该是scanf("%c",&p1->sex);有问题
%c可以输入的字符包括回车、空格等,所以前面的输入最后的回车会影响输入,可以在前面加个getchar();或者在%c前加个空格