#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*DeleteFromBegin(Node*root,int x)
{
if(root==NULL)
return root;
Node*temp=root;
root=root->next;
delete temp;
return root;
}
Node*DeleteFromPos(Node*root,int x,int pos)
{
if(root==NULL || pos<0)
{
return NULL;
}
Node*currnode,*prevnode;
currnode=root;
prevnode=NULL;
int count=0;
while(currnode!=NULL && count<pos)
{
prevnode=currnode;
currnode=currnode->next;
}
if(currnode!=NULL)
{
prevnode->next=currnode->next;
delete currnode;
}
return root;
}
Node*DeleteFromEnd(Node*root,int x)
{
if(root==NULL)
return NULL;
if(root->next==NULL)
return root;
Node*currnode;
currnode=root;
while(currnode->next && currnode->next->next)
{
currnode=currnode->next;
}
Node*temp=currnode->next;
currnode->next=NULL;
delete temp;
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;
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=DeleteFromBegin(root,a[i]);
}
Print(root);
for(int i=0;i<n;i++)
{
root=DeleteFromPos(root,6,0);
}
Print(root);
for(int i=0;i<n;i++)
{
root=DeleteFromEnd(root,4);
}
Print(root);
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKc3RydWN0IE5vZGV7CmludCB2YWw7Ck5vZGUqbmV4dDsKfTsKTm9kZSpJbnNlcnRBdEJlZ2luKE5vZGUqcm9vdCxpbnQgeCkKewp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