#include<bits/stdc++.h>
using namespace std;
struct Node{
int reg;
string s;
float cg;
Node*next;
};
Node*InsertAtBegin(Node*root,int reg,string s,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->s=s;
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 s,float cg,int pos)
{
if(pos==0)
{
root=InsertAtBegin(root,reg,s,cg);
}
else
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->s=s;
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 s,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->reg=reg;
newnode->s=s;
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 s,float cg)
{
Node*newnode=new Node();
newnode->reg=reg;
newnode->s=s;
newnode->cg=cg;
newnode->next=NULL;
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
if(reg<root->reg || s<root->s || cg<root->cg)
{
newnode->next=root;
root=newnode;
return root;
}
while(currnode!=NULL)
{
if(currnode->reg<reg || currnode->s<s || 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 s,float cg)
{
int pos=0;
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
if(currnode->reg==reg && currnode->s==s && currnode->cg==cg)
{
return pos;
}
else
{
currnode=currnode->next;
pos++;
}
}
return -1;
}
Node*Delete(Node*root,int reg,string s,float cg)
{
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
while(currnode!=NULL)
{
if(currnode->reg!=reg && currnode->s!=s && 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<<"s:"<<currnode->s<<"cg:"<<currnode->cg<<endl;
currnode=currnode->next;
}
cout<<endl;
}
int main()
{
Node*root=NULL;
root=InsertAtBegin(root,48,"tu",3.96);
root=InsertAtBegin(root,43,"Cu",3.95);
root=InsertAtBegin(root,98,"ku",3.70);
Print(root);
root=InsertAtPos(root,67,"Mu",3.91,0);
root=InsertAtPos(root,34,"lu",3.57,3);
Print(root);
root=InsertAtEnd(root,95,"au",3.24);
root=InsertAtEnd(root,100,"bu",3.12);
Print(root);
root=SortedInsert(root,56,"fu",4.00);
root=SortedInsert(root,45,"zu",3.98);
root=SortedInsert(root,37,"xu",3.97);
Print(root);
cout<<"Positions of 56,fu,4.00:"<<Search(root,56,"fu",4.00)<<endl;
cout<<"Positions of 2,lu,3.98:"<<Search(root,2,"lu",3.98)<<endl;
root=Delete(root,56,"fu",4.00);
root=Delete(root,105,"ru",3.65);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCByZWc7CnN0cmluZyBzOwpmbG9hdCBjZzsKTm9kZSpuZXh0Owp9OwpOb2RlKkluc2VydEF0QmVnaW4oTm9kZSpyb290LGludCByZWcsc3RyaW5nIHMsZmxvYXQgY2cpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+cmVnPXJlZzsKbmV3bm9kZS0+cz1zOwpuZXdub2Rl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