#include<bits/stdc++.h>
using namespace std;
struct Node{
int reg;
string name;
float cg;
Node*next;
};
Node*InsertAtBegin(Node*root,int reg,string name,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->name=name;
newnode->cg=cg;
if(root==NULL)
{
root=newnode;
return root;
}
else
{
newnode->next=root;
root=newnode;
return root;
}
}
Node*InsertAtPos(Node*root,int reg,string name,float cg,int pos)
{
if(pos==0)
{
root=InsertAtBegin(root,reg,name,cg);
}
else
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->name=name;
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,string name,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->name=name;
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,string name,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->name=name;
newnode->cg=cg;
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
if(reg<currnode->reg || name<currnode->name || cg<currnode->cg)
{
newnode->next=root;
root=newnode;
return root;
}
while(currnode!=NULL)
{
if(currnode->reg<reg || currnode->name<name || 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,string name,float cg)
{
int pos=0;
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
if(currnode->reg==reg && currnode->name==name && currnode->cg==cg)
{
return pos;
}
else
{
currnode=currnode->next;
pos++;
}
}
return -1;
}
Node*Delete(Node*root,int reg,string name,float cg)
{
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
while(currnode!=NULL)
{
if(currnode->reg!=reg && currnode->name!=name && 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<<"name:"<<currnode->name<<"cg:"<<currnode->cg<<endl;
currnode=currnode->next;
}
cout<<endl;
}
int main()
{
Node*root=NULL;
root=InsertAtBegin(root,101,"tu",3.00);
root=InsertAtBegin(root,106,"lu",3.08);
root=InsertAtBegin(root,100,"Mu",3.56);
Print(root);
root=InsertAtPos(root,99,"Nu",3.30,0);
root=InsertAtPos(root,107,"pu",3.67,3);
Print(root);
root=InsertAtEnd(root,108,"ku",3.29);
root=InsertAtEnd(root,109,"yu",3.60);
Print(root);
root=SortedInsert(root,98,"vu",3.45);
root=SortedInsert(root,110,"Xu",2.78);
Print(root);
cout<<"positions of 107,pu,3.67:"<<Search(root,107,"pu",3.67)<<endl;
cout<<"positions of 115,du,3.66:"<<Search(root,115,"du",3.66)<<endl;
root=Delete(root,99,"Nu",3.30);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCByZWc7CnN0cmluZyBuYW1lOwpmbG9hdCBjZzsKTm9kZSpuZXh0Owp9OwpOb2RlKkluc2VydEF0QmVnaW4oTm9kZSpyb290LGludCByZWcsc3RyaW5nIG5hbWUsZmxvYXQgY2cpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+cmVnPXJlZzsKbmV3bm9kZS0+bmFtZT1uYW1lOwpuZXdub2RlLT5jZz1jZzsKaWYocm9vd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