#include<bits/stdc++.h>
using namespace std;
struct Node{
int roll;
float cg;
Node*next;
};
Node*InsertAtBegin(Node*root,int roll,float cg)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->roll=roll;
newnode->cg=cg;
if(root==NULL)
{
root=newnode;
return root;
}
else
{
newnode->next=root;
root=newnode;
return root;
}
}
Node*InsertAtPos(Node*root,int roll,float cg,int pos)
{
if(pos==0)
{
root=InsertAtBegin(root, roll,cg);
}
else
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->roll=roll;
newnode->cg=cg;
Node*currnode;
currnode=root;
for(int i=1;i<pos;i++)
{
currnode=currnode->next;
}
newnode->next=currnode->next;
}
return root;
}
int Search(Node*root,int roll,float cg)
{
int pos=0;
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
if(currnode->roll==roll && currnode->cg==cg)
{
return pos;
}
else
{
currnode=currnode->next;
pos++;
}
}
return -1;
}
void Print(Node*root)
{
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
cout<<currnode->roll<<currnode->cg<<" ";
currnode=currnode->next;
}
cout<<endl;
}
int main()
{
Node*root=NULL;
root=InsertAtBegin(root,3,3.49);
root=InsertAtBegin(root,1,4.00);
root=InsertAtBegin(root,4,3.63);
Print(root);
root=InsertAtPos(root,1,4.00,0);
root=InsertAtPos(root,4,3.63,2);
root=InsertAtPos(root,3,3.49,1);
Print(root);
cout<<"positions of 1,4.00:"<<Search(root,1,4.00)<<endl;
cout<<"positions of 2,4.3:"<<Search(root,2,4.3)<<endl;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCByb2xsOwpmbG9hdCBjZzsKTm9kZSpuZXh0Owp9OwpOb2RlKkluc2VydEF0QmVnaW4oTm9kZSpyb290LGludCByb2xsLGZsb2F0IGNnKQp7Ck5vZGUqbmV3bm9kZT1uZXcgTm9kZSgpOwpuZXdub2RlLT5uZXh0PU5VTEw7Cm5ld25v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