E
Eugen J. Sobchenko
Hi!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition
should be kept for references to previous element of list.
Here is my solution below:
struct node {
int i;
struct node *p; /* prev */
struct node *n; /* next */
};
void swap ( struct node *a1, struct node *a2 ) {
struct node* a1p = a1->p;
struct node* a2p = a2->p;
struct node* a2n_o = a2->n;
struct node* a1n_o = a1->n;
struct node* a1n = a1->n;
struct node* a2n = a2->n;
struct node* a2p_o = a2->p;
struct node* a1p_o = a1->p;
if ( a1->n == a2 ) {
// ...->[ a1 ]->[ a2 ]->...
if ( a1p != NULL ) {
a1p->n = NULL;
}
if ( a2n != NULL ) {
a2n->p = NULL;
}
a2->n = a1;
a1->n = a2n_o;
a1->p = a2;
a2->p = a1p_o;
if ( a1p != NULL ) {
a1p->n = a2;
}
if ( a2n != NULL ) {
a2n->p = a1;
}
} else if ( a2->n == a1 ) {
// ...->[ a2 ]->[ a1 ]->...
if ( a2p != NULL ) {
a2p->n = NULL;
}
if ( a1n != NULL ) {
a1n->p = NULL;
}
a1->n = a2;
a2->n = a1n_o;
a2->p = a1;
a1->p = a2p_o;
if ( a2p != NULL ) {
a2p->n = a1;
}
if ( a1n != NULL ) {
a1n->p = a2;
}
} else {
// ...->[ a1 ]->...->[ a2 ]->...
if ( a2p != NULL ) {
a2p->n = NULL;
}
if ( a1n != NULL ) {
a1n->p = NULL;
}
if ( a1p != NULL ) {
a1p->n = a2;
}
if ( a2n != NULL ) {
a2n->p = a1;
}
if ( a2p != NULL ) {
a2p->n = a1;
}
if ( a1n != NULL ) {
a1n->p = a2;
}
a1->n = NULL;
a2->n = a1n_o;
a1->n = a2n_o;
a2->p = NULL;
a1->p = a2p_o;
a2->p = a1p_o;
}
}
The question is - how to correctly test effectiveness of such implementation
to optimize it? May be some kind of visualisation to make possible to reduce
quantity of steps or something?
Thanks!
I'm writing function which swaps two arbitrary elements
of double-linked list. References to the next element of list
must be unique or NULL (even during swap procedure), the same condition
should be kept for references to previous element of list.
Here is my solution below:
struct node {
int i;
struct node *p; /* prev */
struct node *n; /* next */
};
void swap ( struct node *a1, struct node *a2 ) {
struct node* a1p = a1->p;
struct node* a2p = a2->p;
struct node* a2n_o = a2->n;
struct node* a1n_o = a1->n;
struct node* a1n = a1->n;
struct node* a2n = a2->n;
struct node* a2p_o = a2->p;
struct node* a1p_o = a1->p;
if ( a1->n == a2 ) {
// ...->[ a1 ]->[ a2 ]->...
if ( a1p != NULL ) {
a1p->n = NULL;
}
if ( a2n != NULL ) {
a2n->p = NULL;
}
a2->n = a1;
a1->n = a2n_o;
a1->p = a2;
a2->p = a1p_o;
if ( a1p != NULL ) {
a1p->n = a2;
}
if ( a2n != NULL ) {
a2n->p = a1;
}
} else if ( a2->n == a1 ) {
// ...->[ a2 ]->[ a1 ]->...
if ( a2p != NULL ) {
a2p->n = NULL;
}
if ( a1n != NULL ) {
a1n->p = NULL;
}
a1->n = a2;
a2->n = a1n_o;
a2->p = a1;
a1->p = a2p_o;
if ( a2p != NULL ) {
a2p->n = a1;
}
if ( a1n != NULL ) {
a1n->p = a2;
}
} else {
// ...->[ a1 ]->...->[ a2 ]->...
if ( a2p != NULL ) {
a2p->n = NULL;
}
if ( a1n != NULL ) {
a1n->p = NULL;
}
if ( a1p != NULL ) {
a1p->n = a2;
}
if ( a2n != NULL ) {
a2n->p = a1;
}
if ( a2p != NULL ) {
a2p->n = a1;
}
if ( a1n != NULL ) {
a1n->p = a2;
}
a1->n = NULL;
a2->n = a1n_o;
a1->n = a2n_o;
a2->p = NULL;
a1->p = a2p_o;
a2->p = a1p_o;
}
}
The question is - how to correctly test effectiveness of such implementation
to optimize it? May be some kind of visualisation to make possible to reduce
quantity of steps or something?
Thanks!