#include<bits/stdc++.h>
using namespace std;
struct Node{
int val;
Node*next;
};
Node*InsertAtBegin(Node*root,int x)
{
Node*newnode=new Node();
newnode->val=x;
newnode->next=NULL;
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->val=x;
newnode->next=NULL;
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->val=x;
newnode->next=NULL;
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->val=x;
newnode->next=NULL;
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;
}
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,6);
root=InsertAtBegin(root,1);
Print(root);
root=InsertAtPos(root,9,0);
root=InsertAtPos(root,3,3);
Print(root);
root=InsertAtEnd(root,10);
root=InsertAtEnd(root,12);
Print(root);
cout<<"Position of 12:"<<Search(root,12)<<endl;
root=Delete(root,9);
root=Delete(root,15);
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCB2YWw7Ck5vZGUqbmV4dDsKfTsKTm9kZSpJbnNlcnRBdEJlZ2luKE5vZGUqcm9vdCxpbnQgeCkKewpOb2RlKm5ld25vZGU9bmV3IE5vZGUoKTsKbmV3bm9kZS0+dmFsPXg7Cm5ld25vZGUtPm5leHQ9TlVMTDsKaWYocm9vdD09TlVMTCkKewpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9CmVsc2UKewpuZXdub2RlLT5uZXh0PXJvb3Q7CnJvb3Q9bmV3bm9kZTsKcmV0dXJuIHJvb3Q7Cn0KfQpOb2RlKkluc2VydEF0UG9zKE5vZGUqcm9vdCxpbnQgeCxpbnQgcG9zKQp7CmlmKHBvcz09MCkKewpyb290PUluc2VydEF0QmVnaW4ocm9vdCx4KTsKfQplbHNlCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPnZhbD14OwpuZXdub2RlLT5uZXh0PU5VTEw7Ck5vZGUqY3Vycm5vZGU7CmN1cnJub2RlPXJvb3Q7CmZvcihpbnQgaT0xO2k8cG9zO2krKykKewpjdXJybm9kZT1jdXJybm9kZS0+bmV4dDsKfQpuZXdub2RlLT5uZXh0PWN1cnJub2RlLT5uZXh0OwpjdXJybm9kZS0+bmV4dD1uZXdub2RlOwp9CnJldHVybiByb290Owp9Ck5vZGUqSW5zZXJ0QXRFbmQoTm9kZSpyb290L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