Dave Vandervies wrote:
[]
This is one of my favorites:
#include <stdio.h>
#include <stdlib.h>
typedef struct n{struct n*p1,*p2;int v;}n;n*r_t(n*p,n*i){n*i2,*t1,*t2;int v
;if(!i)return 0;while(p->p2)p=p->p2;i2=i;do{t1=malloc(sizeof*t1);t2=malloc(
sizeof*t2);if(!t1||!t2)return 0;t1->v=t2->v=i2->v;t2->p1=t2->p2=0;t1->p1=i2
;t1->p2=t2;if(t1->p1->p2)t1->p1->p2->p1=t1;if(t1->p1->p1)t1->p1->p1->p1->p2
=t1;i2=t1->p1->p2;}while(i2);i=t1;while(i->p1->p1){i2=i;v=i2->v;while(p->v!=
v&&p->p1->v!=v){i2=i2->p1->p1;if(!i2){fputs("(1)\n",stderr);return 0;}v=i2
->v;}if(v==p->v){if(p->p1){p=p->p1;free(p->p2);p->p2=0;}else{fputs("(2)\n",
stderr);return 0;}if(i2->p1->p1)i2->p1->p1->p1->p2=i2->p1->p2;if(i2->p1->p2
)i2->p1->p2->p1->p1=i2->p1->p1;else i=i2;t1=i2->p2;t2=i2->p1->p1;free(i2->
p1);free(i2);t2->p2->p2=t1;}else{t2=p->p1;if(t2->p1)t2->p1->p2=t2->p2;t2->p2
->p1=t1->p1;free(t2);i2=i2->p1->p1;if(i2->p1->p1)i2->p1->p1->p1->p2=i2->p1->
p2;i2->p1->p2->p1->p1=i2->p1->p1;t1=i2->p2;t2=i2->p1->p2;free(i2->p1);free(
i2);t2->p2->p1=t1;}}if(p->p1||p->p2)fputs("(3)\n",stderr);free(p);if(i->p1->
p1||i->p1->p2)fputs("(4)\n",stderr);t1=i->p2;free(i->p1);free(i);return t1;}
1. main() would have been most welcome :)
2. what if we have something like:
n node1 = { NULL, NULL, 1 };n node2 = { NULL, NULL, 2 };n node = {
NULL, NULL, 3 };
node.p1 = &node2;node.p2 = &node1;node1.p1 = &node;node1.p2 = &node2;
node2.p1 = &node1;node2.p2 = &node;
Is the above input well (-enough, for the above, that is) formed?