输入的时候只能输一组,大神求助
#include<stdio.h>#include<stdlib.h>
#define SIZE sizeof(struct student)
struct student
{
long int num;
char name[20];
char sex;
int age;
struct student *next;
};
struct student *create()
{
struct student *head,*p1,*p2;
head=NULL;
p1=(struct student *)malloc(SIZE);
scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
while(p1->num!=0)
{
if(head=NULL)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(SIZE);
scanf("%ld%s%c%d",&p1->num,p1->name,&p1->sex,&p1->age);
}
p2->next=NULL;
free(p1);
return head;
}
struct student *delet(struct student *head,int num)
{
struct student *p1=head,*p2;
while(p1->num!=num&&p1->next!=NULL)
{
p2=p1;
p1=p1->next;
}
if(p1->num==num)
{
if(p1==head)
head=p1->next;
else
p2->next=p1->next;
}
return head;
}
void print(struct student *head)
{
struct student *p1;
p1=head;
if(head=NULL)
printf("为空表");
else
{
do
{
printf("%ld%s%c%d",p1->num,p1->name,p1->sex,p1->age);
p1=p1->next;
}
while(p1->next!=NULL);
}
}
void main()
{
struct student *head;
long int num;
head=create();
printf("原来的链表:");
print(head);
printf("Please enter you want deleted:");
scanf("%ld",&num);
delet(head,num);
printf("删除后的链表:");
print(head);
}