5.
#include <stdio.h>
#include <stdlib.h>
struct type
{
int x, y;
struct type *new;
};
void walk(struct type * top_ref, int x,int y)
{
struct type* new_type = (struct type*) malloc(sizeof(struct type));
new_type->x = x;
new_type->y = y;
new_type->new = (*top_ref);
(*top_ref) = new_type;
}
void steps(struct type *top)
{
struct type *temp = top;
while (temp != NULL)
{
printf("%d%d", temp->x,temp->y);
temp = temp->new;
}
}
void run(struct type *top, struct type *new)
{
top->new = new->new;
new->new = NULL;
free(new);
}struct type* stop(struct type *top)
{
if (top==NULL || top->new ==NULL || top->new->new==NULL)
return top;
struct type* new = top->new;
struct type *dope = new->new ;
if (top->x == new->x)
{
while (dope !=NULL && new->x==dope->x)
{
run(top, new);
new = dope;
dope = dope->new;
}
}else if (top->y==new->y)
{
while (dope !=NULL && new->y==dope->y)
{
run(top, new);
new = dope;
dope = dope->new;
}
}
else
{
return 0;
}
stop(top->new);
return top;
}
int main()
{
int t,n,a,b,i;
struct type *top;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d%d",&a,&b);
walk(&top, a,b);
}
if (stop(top) == NULL);
{
steps(top);
}
printf("\n");
}
return 0;
}
Input Format
1.Number of test case
2.an integer n denoting number of nodes in linked list, n lines follow
3.in each line two space separated integers
Output Format
two space separated integers in several lines
Sample Input
2
8
2 5
4 5
6 5
8 5
8 3
8 0
9 0
12 0
4
10 10
15 10
20 10
20 5
Sample output
Test case-1
12 0
8 0
8 5
2 5
Test case-2
20 5
20 10
10 10
Constraints :
0< testcase <=10
0 < number of nodes <=10^5
-10^9 <= Elements of nodes <=10^9