#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 || cg<root->cg)
{
newnode->next=root;
root=newnode;
return root;
}
while(currnode!=NULL)
{
if(currnode->reg<reg || currnode->cg<cg)
{
prevnode=currnode;
currnode=currnode->next;
}
else
{
prevnode->next=newnode;
newnode->next=currnode;
return root;
}
}
prevnode->next=newnode;
newnode->next=NULL;
return root;
}
int Search(Node*root,int reg,float cg)
{
int pos=0;
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
if(currnode->reg==reg && currnode->cg==cg)
{
return pos;
}
else
{
currnode=currnode->next;
pos++;
}
}
return -1;
}
Node*Delete(Node*root,int reg,float cg)
{
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
while(currnode!=NULL)
{
if(currnode->reg<reg && currnode->cg<cg)
{
prevnode=currnode;
currnode=currnode->next;
}
else
{
if(currnode==root)
{
root=root->next;
delete(currnode);
}
else
{
prevnode->next=currnode->next;
delete(currnode);
}
break;
}
}
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);
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);
cout<<"Positions of 56,4.00:"<<Search(root,56,4.00)<<endl;
cout<<"Positions of 2,3.98:"<<Search(root,2,3.98)<<endl;
root=Delete(root,56,4.00);
root=Delete(root,105,3.65);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCByZWc7CmZsb2F0IGNnOwpOb2RlKm5leHQ7Cn07Ck5vZGUqSW5zZXJ0QXRCZWdpbihOb2RlKnJvb3QsaW50IHJlZyxmbG9hdCBjZykKewpOb2RlKm5ld25vZGU9bmV3IE5vZGUoKTsKbmV3bm9kZS0+bmV4dD1OVUxMOwpuZXdub2RlLT5yZWc9cmVnOwpuZXdub2RlLT5jZz1jZzsKaWYocm9vdD09TlVMTCkKewpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9CmVsc2UKewpuZXdub2RlLT5uZXh0PXJvb3Q7CnJvb3Q9bmV3bm9kZTsKcmV0dXJuIHJvb3Q7Cn0KfQpOb2RlKkluc2VydEF0UG9zKE5vZGUqcm9vdCxpbnQgcmVnLGZsb2F0IGNnLGludCBwb3MpC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