#include<bits/stdc++.h>
using namespace std;
struct Node{
int reg;
float cg;
Node*next;
};
Node*InsertAtBegin(Node*root,int reg,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->cg=cg;
if(root==NULL)
{
root=newnode;
return root;
}
else
{
newnode->next=root;
root=newnode;
return root;
}
}
Node*InsertAtPos(Node*root,int reg,float cg,int pos)
{
if(pos==0)
{
root=InsertAtBegin(root,reg,cg);
}
else
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->cg=cg;
Node*currnode;
currnode=root;
for(int i=1;i<pos;i++)
{
currnode=currnode->next;
}
newnode->next=currnode->next;
currnode->next=newnode;
}
return root;
}
Node*InsertAtEnd(Node*root,int reg,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->cg=cg;
if(root==NULL)
{
root=newnode;
return root;
}
Node*currnode;
currnode=root;
while(currnode->next=NULL)
{
currnode=currnode->next;
}
currnode->next=newnode;
return root;
}
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=InsertAtBegin(root,48,3.96);
root=InsertAtBegin(root,43,3.95);
root=InsertAtBegin(root,98,3.70);
Print(root);
root=InsertAtPos(root,67,3.91,0);
root=InsertAtPos(root,34,3.57,3);
Print(root);
root=InsertAtEnd(root,95,3.24);
Print(root);
root=InsertAtEnd(root,100,3.12);
Print(root);
root=SortedInsert(root,56,4.00);
root=SortedInsert(root,45,3.98);
root=SortedInsert(root,34,3.97);
Print(root);
}
CiNpbmNsdWRlPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnN0cnVjdCBOb2RlewppbnQgcmVnOwpmbG9hdCBjZzsKTm9kZSpuZXh0Owp9OwpOb2RlKkluc2VydEF0QmVnaW4oTm9kZSpyb290LGludCByZWcsZmxvYXQgY2cpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+cmVnPXJlZzsKbmV3bm9kZS0+Y2c9Y2c7CmlmKHJvb3Q9PU5VTEwpCnsKcm9vdD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQplbHNlCnsKbmV3bm9kZS0+bmV4dD1yb290Owpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9Cn0KTm9kZSpJbnNlcnRBdFBvcyhOb2RlKnJvb3QsaW50IHJlZyxmbG9hdCBjZyxpbnQgcG9zKQp7CmlmKHBvcz09MCkKewpyb290PUluc2VydEF0QmVnaW4ocm9vdCxyZWcsY2cpOwp9CmVsc2UKewpOb2RlKm5ld25vZGU9bmV3IE5vZGUoKTsKbmV3bm9kZS0+bmV4dD1OVUxMOwpuZXdub2RlLT5yZWc9cmVnOwpuZXdub2RlLT5jZz1jZzsKTm9kZSpjdXJybm9kZTsKY3Vycm5vZGU9cm9vdDsKZm9yKGludCBpPTE7aTxwb3M7aSsrKQp7CmN1cnJub2RlPWN1cnJub2RlLT5uZXh0Owp9Cm5ld25vZGUtPm5leHQ9Y3Vycm5vZGUtPm5leHQ7CmN1cnJub2RlLT5uZXh0PW5ld25vZGU7Cn0KcmV0dXJuIHJvb3Q7Cn0KTm9kZSpJbnNlcnRBdEVuZChOb2RlKnJvb3QsaW50IHJlZyxmbG9hdC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