#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;i++)
{
currnode=currnode->next;
}
newnode->next=currnode->next;
currnode->next=newnode;
}
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);
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpzdHJ1Y3QgTm9kZSB7CiAgICBpbnQgdmFsOwogICAgTm9kZSogbmV4dDsKfTsKCgpOb2RlKiBJbnNlcnRBdEJlZ2luKE5vZGUqIHJvb3QsIGludCB4KSB7CiAgICBOb2RlKiBuZXdub2RlID0gbmV3IE5vZGUoKTsKICAgIG5ld25vZGUtPnZhbCA9IHg7CiAgICBuZXdub2RlLT5uZXh0ID0gTlVMTDsK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ycm5vZGUtPm5leHQ7CiAgICB9CiAgICBjb3V0IDw8IGVuZGw7Cn0KCmludCBtYWluKCkgewogICAgTm9kZSogcm9vdCA9IE5VTEw7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOyAKaWYobjw9MCkKewpjb3V0PDxlbmRsOwpyZXR1cm4gMDsKfQogICAgaW50IGFbbl07CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgIGNpbiA+PiBhW2ldOyAKICAgIH0KCiAgICBQcmludChyb290KTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgcm9vdCA9IEluc2VydEF0QmVnaW4ocm9vdCwgYVtpXSk7CiAgICB9CgogICAgCiAgICBQcmludChyb290KTsKZm9yKGludCBpPTA7aTxuO2krKykKewpyb290PUluc2VydEF0RW5kKHJvb3QsYVtpXSk7Cn0KUHJpbnQocm9vdCk7Cgpyb290PUluc2VydEF0UG9zKHJvb3QsNSwwKTsKcm9vdD1JbnNlcnRBdFBvcyhyb290LDgsMyk7CgpQcmludChyb290KTsKICAgIHJldHVybiAwOwp9