#include <stdio.h>
#include <stdlib.h>
typedef
struct
Node {
struct
Node *left, *right;
int
data;
} Node;
Node* concatenate(Node* leftList, Node* rightList)
{
if
(leftList == NULL)
return
rightList;
if
(rightList == NULL)
return
leftList;
Node* leftLast = leftList->left;
Node* rightLast = rightList->left;
leftLast->right = rightList;
rightList->left = leftLast;
leftList->left = rightLast;
rightLast->right = leftList;
return
leftList;
}
Node* bTreeToCList(Node* root)
{
if
(root == NULL)
return
NULL;
Node* left = bTreeToCList(root->left);
Node* right = bTreeToCList(root->right);
root->left = root->right = root;
return
concatenate(concatenate(left, root), right);
}
void
displayCList(Node* head)
{
printf
(
"Circular Linked List is :\n"
);
Node* itr = head;
do
{
printf
(
"%d "
, itr->data);
itr = itr->right;
}
while
(head != itr);
printf
(
"\n"
);
}
Node* newNode(
int
data)
{
Node* temp = (Node*)
malloc
(
sizeof
(Node));
temp->data = data;
temp->left = temp->right = NULL;
return
temp;
}
int
main()
{
Node* root = newNode(10);
root->left = newNode(12);
root->right = newNode(15);
root->left->left = newNode(25);
root->left->right = newNode(30);
root->right->left = newNode(36);
Node* head = bTreeToCList(root);
displayCList(head);
return
0;
}