#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*InsertAtEnd(Node*root,int x)
{
Node*newnode=new Node();
newnode->next=NULL;
newnode->val=x;
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 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,6);
root=InsertAtBegin(root,2);
root=InsertAtBegin(root,8);
Print(root);
root=InsertAtPos(root,9,0);
root=InsertAtPos(root,5,3);
root=InsertAtPos(root,1,4);
Print(root);
root=InsertAtEnd(root,7);
root=InsertAtEnd(root,0);
root=InsertAtEnd(root,3);
Print(root);
root=SortedInsert(root,4);
root=SortedInsert(root,9);
root=SortedInsert(root,2);
Print(root);
cout<<"Positions of 3:"<<Search(root,3)<<endl;
cout<<"Positions of 13:"<<Search(root,13)<<endl;
root=Delete(root,6);
root=Delete(root,12);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGUKewppbnQgdmFsOwpOb2RlKm5leHQ7Cn07Ck5vZGUqSW5zZXJ0QXRCZWdpbihOb2RlKnJvb3QsaW50IHgpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+dmFsPXg7CmlmKHJvb3Q9PU5VTEwpCnsKcm9vdD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQplbHNlCnsKbmV3bm9kZS0+bmV4dD1yb290Owpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9Cn0KTm9kZSpJbnNlcnRBdFBvcyhOb2RlKnJvb3QsaW50IHgsaW50IHBvcykKewppZihwb3M9PTApCnsKcm9vdD1JbnNlcnRBdEJlZ2luKHJvb3QseCk7Cn0KZWxzZQp7Ck5vZGUqbmV3bm9kZT1uZXcgTm9kZSgpOwpuZXdub2RlLT5uZXh0PU5VTEw7Cm5ld25vZGUtPnZhbD14OwpOb2RlKmN1cnJub2RlOwpjdXJybm9kZT1yb290Owpmb3IoaW50IGk9MTtpPHBvcztpKyspCnsKY3Vycm5vZGU9Y3Vycm5vZGUtPm5leHQ7Cn0K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