#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node*next;
};
Node*InsertAtBegin(Node*root,int x)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->val=x;
if(root==NULL)
{
root=newnode;
return root;
}
else
{
newnode->next=root;
root=newnode;
return root;
}
}
Node*InsertAtPos(Node*root,int x,int pos)
{
if(pos==0)
{
root=InsertAtBegin(root,x);
}
else
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->val=x;
Node*currnode;
currnode=root;
for(int i=1;i<pos;i++)
{
currnode=currnode->next;
}
newnode->next=currnode->next;
currnode->next=newnode;
}
return root;
}
Node*SortedInsert(Node*root,int x)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->val=x;
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
if(root==NULL)
{
root=newnode;
return root;
}
if(x<root->val)
{
newnode->next=root;
root=newnode;
return root;
}
while(currnode!=NULL)
{
if(currnode->val<x)
{
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 x)
{
int pos=0;
Node*currnode;
currnode=root;
while(currnode!=NULL)
{
if(currnode->val==x)
{
return pos;
}
else
{
currnode=currnode->next;
pos++;
}
}
return -1;
}
Node*Delete(Node*root,int x)
{
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
while(currnode!=NULL)
{
if(currnode->val!=x)
{
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<<currnode->val<<" ";
currnode=currnode->next;
}
cout<<endl;
}
int main()
{
Node*root=NULL;
root=InsertAtBegin(root,4);
root=InsertAtBegin(root,7);
root=InsertAtBegin(root,2);
root=InsertAtPos(root,9,3);
root=InsertAtPos(root,1,0);
root=InsertAtPos(root,5,2);
root=SortedInsert(root,3);
root=SortedInsert(root,8);
root=SortedInsert(root,6);
cout<<"position of 1"<<Search(root,1)<<endl;
cout<<"positions of 12"<<Search(root,12)<<endl;
root=Delete(root,8);
root=Delete(root,11);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCB2YWw7Ck5vZGUqbmV4dDsKfTsKTm9kZSpJbnNlcnRBdEJlZ2luKE5vZGUqcm9vdCxpbnQgeCkKewpOb2RlKm5ld25vZGU9bmV3IE5vZGUoKTsKbmV3bm9kZS0+bmV4dD1OVUxMOwpuZXdub2RlLT52YWw9eDsKaWYocm9vdD09TlVMTCkKewpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9CmVsc2UKewpuZXdub2RlLT5uZXh0PXJvb3Q7CnJvb3Q9bmV3bm9kZTsKcmV0dXJuIHJvb3Q7Cn0KfQpOb2RlKkluc2VydEF0UG9zKE5vZGUqcm9vdCxpbnQgeCxpbnQgcG9zKQp7CmlmKHBvcz09MCkKewpyb290PUluc2VydEF0QmVnaW4ocm9vdCx4KTsKfQplbHNlCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+dmFsPXg7Ck5vZGUqY3Vycm5vZGU7CmN1cnJub2RlPXJvb3Q7CmZvcihpbnQgaT0xO2k8cG9zO2krKykKewpjdXJybm9kZT1jdXJybm9kZS0+bmV4dDsKfQpuZXdub2RlLT5uZXh0PWN1cnJub2RlLT5uZXh0OwpjdXJybm9kZS0+bmV4dD1uZXdub2RlOwp9CnJldHVybiByb290Owp9Ck5vZGUqU29ydGVkSW5zZXJ0KE5vZGUqcm9vdCxpbnQgeCkKewpOb2RlKm5ld25vZGU9bmV3IE5vZGUoKTsKbmV3bm9kZS0+bmV4dD1OVUxMOwpuZXdub2RlLT52YWw9eDsKTm9kZSpjd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