#include<bits/stdc++.h>
using namespace std;
struct Node{
string name;
Node*next;
};
Node*InsertAtBegin(Node*root,string name)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->name=name;
if(root==NULL)
{
root=newnode;
return root;
}
else
{
newnode->next=root;
root=newnode;
return root;
}
}
Node*InsertAtPos(Node*root,string name,int pos)
{
if(pos==0)
{
root=InsertAtBegin(root,name);
}
else
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->name=name;
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,string name)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->name=name;
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,string name)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->name=name;
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
if(name<currnode->name)
{
newnode->next=root;
root=newnode;
return root;
}
while(currnode!=NULL)
{
if(currnode->name<name)
{
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,string name)
{
int pos=0;
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
if(currnode->name==name)
{
return pos;
}
else
{
currnode=currnode->next;
pos++;
}
}
return -1;
}
Node*Delete(Node*root,string name)
{
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
while(currnode!=NULL)
{
if(currnode->name!=name)
{
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<<"name:"<<currnode->name<<endl;
currnode=currnode->next;
}
cout<<endl;
}
int main()
{
Node*root=NULL;
root=InsertAtBegin(root,"tu");
root=InsertAtBegin(root,"lu");
root=InsertAtBegin(root,"Mu");
Print(root);
root=InsertAtPos(root,"Nu",0);
root=InsertAtPos(root,"pu",3);
Print(root);
root=InsertAtEnd(root,"ku");
root=InsertAtEnd(root,"yu");
Print(root);
root=SortedInsert(root,"vu");
root=SortedInsert(root,"Xu");
Print(root);
cout<<"positions of pu:"<<Search(root,"pu")<<endl;
cout<<"positions of du:"<<Search(root,"du")<<endl;
root=Delete(root,"Nu");
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CnN0cmluZyBuYW1lOwpOb2RlKm5leHQ7Cn07Ck5vZGUqSW5zZXJ0QXRCZWdpbihOb2RlKnJvb3Qsc3RyaW5nIG5hbWUpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+bmFtZT1uYW1lOwppZihyb290PT1OVUxMKQp7CnJvb3Q9bmV3bm9kZTsKcmV0dXJuIHJvb3Q7Cn0KZWxzZQp7Cm5ld25vZGUtPm5leHQ9cm9vdDsKcm9vdD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQp9Ck5vZGUqSW5zZXJ0QXRQb3MoTm9kZSpyb290LHN0cmluZyBuYW1lLGludCBwb3MpCnsKaWYocG9zPT0wKQp7CnJvb3Q9SW5zZXJ0QXRCZWdpbihyb290LG5hbWUpOwp9CmVsc2UKewpOb2RlKm5ld25vZGU9bmV3IE5vZGUoKTsKbmV3bm9kZS0+bmV4dD1OVUxMOwpuZXdub2RlLT5uYW1lPW5hbWU7Ck5vZGUqY3Vycm5vZGU7CmN1cnJub2RlPXJvb3Q7CmZvcihpbnQgaT0xO2k8cG9zO2krKykKewpjdXJybm9kZT1jdXJ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