#include<stdio.h>
#include<alloc.h>
#define NULL 0
#define len sizeof(struct student)
struct date
{
int year,month,day;
};
struct student
{
long num;
char name[20];
char sex;
struct date age;
struct student *next;
};
int n;
struct student *creat(void)
{
struct student *head;
struct student *p1,*p2;
head=NULL;
n=0;
p1=p2=(struct student *)malloc(len);
printf("please input information:/n/n");
printf("num:");
scanf("%ld",&p1->num);
printf("name:");
scanf("%s",p1->name);
getchar();
printf("sex:");
p1->sex=getchar();
printf("age:");
scanf("%d%d%d",&p1->age.year,&p1->age.month,&p1->age.day);
while(p1->num!=-1)
{
n++;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct student *)malloc(len);
printf("num:");
scanf("%ld",&p1->num);
printf("name:");
scanf("%s",p1->name);
getchar();
printf("sex:");
p1->sex=getchar();
printf("age:");
scanf("%d%d%d",&p1->age.year,&p1->age.month,&p1->age.day);
}
p2->next=NULL;
return (head);
}
void print(struct student *p)
{
if(p->next!=NULL)
do
{
printf("The result is: num:%ld name:%s sex:%c age:%d %d %d/n",p->num,p->name,p->sex,p->age.year,p->age.month,p->age.day);
p=p->next;
}while(p!=NULL);
}
struct student *del(struct student *head,long num)
{
struct student *p1,*p2;
if(head==NULL)printf("/nList NULL!/n");
p1=head;
while(num!=p1->num&&p1->next!=NULL&&head!=NULL)
{
p2=p1;
p1=p1->next;
}
if(num==p1->num)
{
if(p1==head)head=p1->next;
else p2->next=p1->next;
printf("have del./n");
n--;
}
else printf("Not found!/n");
return head;
}
void main()
{
struct student *stud;
long num;
clrscr();
stud=creat();
print(stud);
printf("Input a num to del:/n");
scanf("%ld",&num);
stud=del(stud,num);
print(stud);
}