#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*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*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 && currnode!=NULL;i++)
{
currnode=currnode->next;
}
newnode->next=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;
}
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 = root;
while (currnode != NULL) {
cout << currnode->val << " ";
currnode = currnode->next;
}
cout << endl;
}
int main() {
Node* root = NULL;
int n;
cin >> n;
if(n<=0)
{
cout<<endl;
return 0;
}
int a[n];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
Print(root);
for (int i = 0; i < n; i++) {
root = InsertAtBegin(root, a[i]);
}
Print(root);
for(int i=0;i<n;i++)
{
root=InsertAtEnd(root,a[i]);
}
Print(root);
root=InsertAtPos(root,5,0);
root=InsertAtPos(root,8,3);
Print(root);
for(int i=0;i<n;i++)
{
root=SortedInsert(root,a[i]);
}
Print(root);
for(int i=0;i<n;i++)
{
cout<<"Positions of "<<a[i]<<":"<<Search(root,a[i])<<endl;
}
for(int i=0;i<n;i++)
{
root=Delete(root,a[i]);
}
Print(root);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTm9kZSB7CiAgICBpbnQgdmFsOwogICAgTm9kZSogbmV4dDsKfTsKCgpOb2RlKiBJbnNlcnRBdEJlZ2luKE5vZGUqIHJvb3QsIGludCB4KSB7CiAgICBOb2RlKiBuZXdub2RlID0gbmV3IE5vZGUoKTsKICAgIG5ld25vZGUtPnZhbCA9IHg7CiAgICBuZXdub2RlLT5uZXh0ID0gTlVMTDsKaWYocm9vdD09TlVMTCkKewpyb290PW5ld25vZGU7CiAgICByZXR1cm4gcm9vdDsKfQplbHNlCnsKbmV3bm9kZS0+bmV4dD1yb290Owpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9Cn0KTm9kZSpJbnNlcnRBdEVuZChOb2RlKnJvb3QsaW50IHgpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+dmFsPXg7CmlmKHJvb3Q9PU5VTEwpCnsKcm9vdD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQpOb2RlKmN1cnJub2RlOwpjdXJybm9kZT1yb290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