/*链表 创建,插入,输出*/
#include<stdio.h>
#include<stdlib.h>
struct student
{
__int64 num;
char sex;
char name[10];
char come_from[20];
struct student *next;
};/*建立结构体*/
void out(struct student *head)
{
struct student *p;
p=head;
do
{
printf("%I64d %c %s %s\n",p->num,p->sex,p->name,p->come_from);
p=p->next; }while(p!=NULL);
} /*输出*/
struct student *creat()
{
int m,n;
struct student *head=NULL,*p,*q,*t;
scanf("%d",&n);
while(n--)
{
p=(struct student*) malloc(sizeof(struct student));
scanf("%I64d %c %s %s",&p->num,&p->sex,p->name,p->come_from);
if(head==NULL)
head=p;
else
q->next=p;
q=p;
}
q->next=NULL;
return(head);
}/*建立链表*/
struct student *input(struct student *head,int m)
{
struct student *p,*q,*t;
int i;
for(i=0;i<m;i++)
{
p=(struct student*) malloc(sizeof(struct student));
scanf("%I64d %c %s %s",&p->num,&p->sex,p->name,p->come_from);
for(q=head;q!=NULL;q=q->next)
{
if(p->num<q->num)
{
p->next=q;
if(q==head)
head=p;
else t->next=p;
break;
}
t=q;
}
if(q==NULL)
{
t->next=p;
p->next=NULL;
}
}
return (head);
}/*进行有序插入*/
void main()
{
struct student *head;
int m;
head=creat();
scanf("%d",&m);
head=input(head,m);
out(head);
}
链表 插入
最新推荐文章于 2021-03-26 05:26:39 发布