#include "stdio.h"
#include "malloc.h"
#define LEN sizeof(struct student)
struct student
{
int num;
char xm[20];
char xb;
int fs;
char dz[30];
struct student *next;
};
int n;
struct student *creat()
{ struct student *head;
struct student *p1,*p2;
n=0;
p1=p2=(struct student *) malloc(LEN);
printf("请输入数据:\n");
scanf("%d%s%c%d%s",&p1->num,&p1->xm,&p1->xb,&p1->fs,&p1->dz);
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);
scanf("%d%s%c%d%s",&p1->num,&p1->xm,&p1->xb,&p1->fs,&p1->dz);
}
p2->next=NULL;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
if(head!=NULL)
do{
printf("%d %s %c %d %s\n",p->num,p->xm,p->xb,p->fs,p->dz);
p=p->next;
}while(p!=NULL);
}
struct student *insert(struct student *head,struct student *nw)
{
struct student *p0,*p1,*p2;
p0=nw;
p1=head;
if(head==NULL)
{head=p0;p0->next=NULL;}
while((p0->num>p1->num)&&(p1->next!=NULL))
{p2=p1;p1=p1->next;}
if(p0->numnum)
{
if(head==p1)
head=p0;
else
p2->next=p0;
p0->next=p1;
}
else
{
p1->next=p0;p0->next=NULL;
}
n=n+1;
return(head);
}
void main()
{
struct student *head,*nw;
head=creat();
puts("请输入要插入新结点的数据:");
nw=(struct student *)malloc(LEN);
scanf("%d%s%s%d%s",&nw->num,&nw->xm,&nw->xb,&nw->fs,&nw->dz);
nw->next=NULL;
insert(head,nw);
puts("插入后新的数据为:\n");
print(head);
}
输入要插入的结点后,最后出不来结果……
|