Problem Description
Input
Output
Example Input
4
1 1
1 2
0 3
100 4
Example Output
3 1 2 4
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *create(int n)
{
struct node *head,*p,*tail,*q;
int i,x,len,j;
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
tail=head;
len=0;
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
scanf("%d %d",&x,&p->data);
p->next=NULL;
if(x>=len)
{
tail->next=p;
tail=p;
len++;
}
else if(x==0)
{
p->next=head->next;
head->next=p;
len++;
}
else if(x>0&&x<len)
{
q=head;
for(j=0;j<x;j++)
{
q=q->next;
}
p->next=q->next;
q->next=p;
len++;
}
}
return (head);
};
void print(struct node *h)
{
struct node *p=h->next;
while(p!=NULL)
{
if(p->next==NULL)
printf("%d",p->data);
else
printf("%d ",p->data);
p=p->next;
}
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
struct node *p;
p=create(n);
print(p);
printf("\n");
}
return 0;
}