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 *creatData(int m,int x,struct node *head)
{
struct node *p,*q,*tail;
p=(struct node*)malloc(sizeof(struct node));
p->data=x;
p->next=NULL;
q=head->next;
tail=head;
while(q!=NULL&&m--)
{
q=q->next;
tail=tail->next;
}
if(m)
{
tail->next=p;
p->next=q;
}
else
{
tail->next=p;
p->next=NULL;
}
return head;
};
void printfData(struct node *head)
{
struct node *p;
p=head->next;
while(p!=NULL)
{
if(p->next==NULL)
printf("%d\n",p->data);
else
printf("%d ",p->data);
p=p->next;
}
}
int main()
{
struct node *head;
int n,m,x;
while(~scanf("%d",&n))
{
head=(struct node*)malloc(sizeof(struct node));
head->next=NULL;
while(n--)
{
scanf("%d%d",&m,&x);
head=creatData(m,x,head);
}
printfData(head);
}
return 0;
}