#include<iostream>
using namespace std;
typedef struct circular{
int data;
struct circular *next, *rear;
}Circular, *Linklist;
void initclist(Linklist *CL)
{
*CL = new Circular;
(*CL)->next = NULL;
}
void inpclist(Linklist CL)
{
Circular *s;
int a;
CL->rear = CL;
cin >> a;
while(a != -1)
{
s = new Circular;
s->data = a;
CL->rear->next = s;
CL->rear = s;
cin >> a;
}
CL->rear->next = CL;
}
void outpclist(Linklist CL)
{
Circular *r;
r = CL->next;
while(r != CL)
{
cout << r->data << " ";
r = r->next;
}
}
Linklist addfromhead(Linklist LA, Linklist LB)
{
Circular *p, *q;
p = LA;
q = LB;
while(p->next != LA)
p = p->next;
while(q->next != LB)
q = q->next;
q->next = LA;
p->next = LB->next;
free(LB);
return (LA);
}
Linklist addfromend(Linklist LA, Linklist LB)
{
LA->rear->next = LB->next;
LB->rear->next = LA;
free(LB);
return (LA);
}
int main()
{
Linklist LA, LB;
initclist(&LA);
initclist(&LB);
inpclist(LA);
inpclist(LB);
outpclist(LA);
cout << "\n";
outpclist(LB);
cout << "\n";
outpclist(addfromend(LA, LB));
return 0;
}