#include<iostream.h>
struct student
{
int age;
float score;
student* next;
};
struct student *Create()
{
struct student *head,*p1,*p2;
int n=0;
p1=p2=new student;
cout<<"age and score:";
cin>>p1->age>>p1->score;
head=NULL;
while(p1->age!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=new student;
cout<<"age and score:";
cin>>p1->age>>p1->score;
}
p2->next=NULL;
return head;
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL)
{
cout<<p->age<<" "<<p->score<<endl;
p=p->next;
}
}
struct student *insert(struct student *head,struct student *stud)
{
struct student *p0,*p1,*p2;
p1=head;
p0=stud;
int n=0;
if(head==NULL)
{
head = p0;
p0->next = NULL;
}
else
while((p0->age>p1->age)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->age<=p1->age)
{
if(head==p1)
{
head = p1;
p0->next=p1;
}
else
{
p1->next=p0;
p0->next=NULL;
}
}
n=n+1;
return head;
}
void main(void)
{
student *s,*t;
s=Create();
print(s);
cout<<"insert age and score: ";
t=new student;
cin>>t->age>>t->score;
s=insert(s,t);
print(s);
delete t;
}