#include<bits/stdc++.h>
using namespace std;
struct Node{
int reg;
float cg;
Node*next;
};
Node*SortedInsert(Node*root,int reg,float cg)
{
Node*newnode=new Node();
newnode->reg=reg;
newnode->cg=cg;
newnode->next=NULL;
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
if(reg<root->reg)
{
newnode->next=root;
root=newnode;
return root;
}
while(currnode!=NULL)
{
if(currnode->reg<reg)
{
prevnode=currnode;
currnode=currnode->next;
}
else
{
prevnode->next=newnode;
newnode->next=currnode;
return root;
}
}
prevnode->next=newnode;
newnode->next=NULL;
return root;
}
void Print(Node*root)
{
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
cout<<"reg:"<<currnode->reg<<"cg:"<<currnode->cg<<endl;
currnode=currnode->next;
}
cout<<endl;
}
int main()
{
Node*root=NULL;
root=SortedInsert(root,56,4.00);
root=SortedInsert(root,45,3.98);
root=SortedInsert(root,34,3.97);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCByZWc7CmZsb2F0IGNnOwpOb2RlKm5leHQ7Cn07Ck5vZGUqU29ydGVkSW5zZXJ0KE5vZGUqcm9vdCxpbnQgcmVnLGZsb2F0IGNnKQp7Ck5vZGUqbmV3bm9kZT1uZXcgTm9kZSgpOwpuZXdub2RlLT5yZWc9cmVnOwpuZXdub2RlLT5jZz1jZzsKbmV3bm9kZS0+bmV4dD1OVUxMOwpOb2RlKmN1cnJub2RlLCpwcmV2bm9kZTsKY3Vycm5vZGU9cm9vdDsKcHJldm5vZGU9TlVMTDsKaWYocm9vdD09TlVMTCkKewpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9CmlmKHJlZzxyb290LT5yZWcpCnsKbmV3bm9kZS0+bmV4dD1yb290Owpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9CndoaWxlKGN1cnJub2RlIT1OVUxMKQp7CmlmK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