#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;
}
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;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTm9kZSB7CiAgICBpbnQgdmFsOwogICAgTm9kZSogbmV4dDsKfTsKCgpOb2RlKiBJbnNlcnRBdEJlZ2luKE5vZGUqIHJvb3QsIGludCB4KSB7CiAgICBOb2RlKiBuZXdub2RlID0gbmV3IE5vZGUoKTsKICAgIG5ld25vZGUtPnZhbCA9IHg7CiAgICBuZXdub2RlLT5uZXh0ID0gTlVMTDsKaWYocm9vdD09TlVMTCkKewpyb290PW5ld25vZGU7CiAgICByZXR1cm4gcm9vdDsKfQplbHNlCnsKbmV3bm9kZS0+bmV4dD1yb290Owpyb290PW5ld25vZGU7CnJldHVybiByb290Owp9Cn0KTm9kZSpJbnNlcnRBdEVuZChOb2RlKnJvb3QsaW50IHgpCnsKTm9kZSpuZXdub2RlPW5ldyBOb2RlKCk7Cm5ld25vZGUtPm5leHQ9TlVMTDsKbmV3bm9kZS0+dmFsPXg7CmlmKHJvb3Q9PU5VTEwpCnsKcm9vdD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQpOb2RlKmN1cnJub2RlOwpjdXJybm9kZT1yb290Owp3aGlsZShjdXJybm9kZS0+bmV4dCE9TlVMTCkKewpjdXJybm9kZT1jdXJybm9kZS0+bmV4dDsKfQpjdXJybm9kZS0+bmV4dD1uZXdub2RlOwpyZXR1cm4gcm9vdDsKfQpOb2RlKkluc2VydEF0UG9zKE5vZGUqcm9vdCxpbnQgeCxpbnQgcG9zKQp7CmlmKHBvcz09MCkKewpyb290PUluc2VydEF0QmVn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mN1cnJub2RlPXJvb3Q7CndoaWxlKGN1cnJub2RlIT1OVUxMKQp7CmlmKGN1cnJub2RlLT52YWw9PXgpCnsKcmV0dXJuIHBvczsKfQplbHNlCnsKY3Vycm5vZGU9Y3Vycm5vZGUtPm5leHQ7CnBvcysrOwp9Cn0KcmV0dXJuIC0xOwp9CnZvaWQgUHJpbnQoTm9kZSogcm9vdCkgewogICAgTm9kZSogY3Vycm5vZGUgPSByb290OwogICAgd2hpbGUgKGN1cnJub2RlICE9IE5VTEwpIHsKICAgICAgICBjb3V0IDw8IGN1cnJub2RlLT52YWwgPDwgIiAiOwogICAgICAgIGN1cnJub2RlID0gY3Vycm5vZGUtPm5leHQ7CiAgICB9CiAgICBjb3V0IDw8IGVuZGw7Cn0KCmludCBtYWluKCkgewogICAgTm9kZSogcm9vdCA9IE5VTEw7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOyAKaWYobjw9MCkKewpjb3V0PDxlbmRsOwpyZXR1cm4gMDsKfQogICAgaW50IGFbbl07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGNpbiA+PiBhW2ldOyAKICAgIH0KCiAgICBQcmludChyb290KTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgcm9vdCA9IEluc2VydEF0QmVnaW4ocm9vdCwgYVtpXSk7CiAgICB9CgogICAgCiAgICBQcmludChyb290KTsKZm9yKGludCBpPTA7aTxuO2krKykKewpyb290PUluc2VydEF0RW5kKHJvb3QsYVtpXSk7Cn0KUHJpbnQocm9vdCk7Cgpyb290PUluc2VydEF0UG9zKHJvb3QsNSwwKTsKcm9vdD1JbnNlcnRBdFBvcyhyb290LDgsMyk7CgpQcmludChyb290KTsKZm9yKGludCBpPTA7aTxuO2krKykKewpyb290PVNvcnRlZEluc2VydChyb290LGFbaV0pOwp9ClByaW50KHJvb3QpOwpmb3IoaW50IGk9MDtpPG47aSsrKQp7CmNvdXQ8PCJQb3NpdGlvbnMgb2YgIjw8YVtpXTw8IjoiPDxTZWFyY2gocm9vdCxhW2ldKTw8ZW5kbDsKfQogICAgcmV0dXJuIDA7Cn0=