I wrote the following function, please tell me if she is ok.
Code:
struct node{
int info;
struct node * parent;
};
typedef struct node node;
void sameParent (node * n1, node * n2)
{
node * save_n2 = n2;
while (n1 != NULL)
{
n2=save_n2;
while(n2 != NULL)
{
if (n1 == n2)
{
printf ("%d",n1->info);
return;
}
else
n2=n2->parent;
}
n1=n1->parent;
}
printf ("No shared parent");
}
1) The function should print the share parent or "No shared parent" when is'nt exist.
2) Can someone offer more effective code ? this one is O (n^2)