Velocity Reviews > double-linked list elements swap

Eugen J. Sobchenko
Guest
Posts: n/a

 11-04-2004
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!

Method Man
Guest
Posts: n/a

 11-04-2004

"Eugen J. Sobchenko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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!

Just swap the data instead of all the pointers:

void swap ( struct node *a1, struct node *a2 ) {
int temp;
temp = a1->i;
a1->i = a2->i;
a2->i = temp;
}

kevin.bagust
Guest
Posts: n/a

 11-04-2004
In article <(E-Mail Removed) >,
Eugen J. Sobchenko <(E-Mail Removed)> wrote:
> 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.

void swap( struct node *a1, struct node *a2 ) {
int temp;

temp = a1->i;
a1->i = a2->i;
a2->i = temp;
}

more below:

> 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?

I would suggest writing a check double linked list function to which you
pass the head and tail pointers of the linked list, and a constant array
of ints which list the order of the nodes. Then the function simply scans
the linked list starting with the head and compares each i value with the
next value off of the array, if any of the values are incorrect it then
outputs an error, and then reverses the procedure checking from the tail
to the start of the list.

Then write some code to create a list, and perform some node swaps on it
checking after each swap that the list is correct.

Your swap function looks too complicated. If I was writing it I would say
there are two main cases:
1) The two nodes to swap are next to each other,
2) The two nodes to swap are not next to each other

Then to handle the first case you need to:
1) Remove the first node to swap from the linked list
2) Insert the removed node after the second node to swap

And to handle the second case you need to:
1) Remove one of the nodes to swap from the linked list
2) Insert the removed node after the other node
3) Remove the other node from the linked list
4) Insert the removed node where the original removed node was.

Then write functions to remove a specified node from the linked list, and
to insert a node into it.

Kevin.

Giorgos Keramidas
Guest
Posts: n/a

 11-04-2004
On 2004-11-04 01:24, Eugen J. Sobchenko wrote:
> 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.

The constraint of keeping the references NULL or unique during the swap is
something you cannot guarantee by reordering the assignments of the pointers.
Some sort of locking must be used, which is machine-dependent and cannot be
described portably here (i.e. a mutex that protects the entire list, while
reordering operations happen).

One trick that you can use to effectively 'swap' two elements of a list is to
decouple the list linkage from the element data, using something like this:

------------------------------------------------------------------------------
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

struct nodeinfo;
struct node {
struct nodeinfo *n_info;
struct node *n_next;
struct node *n_prev;
};

struct nodeinfo {
int ni_data;
};

struct nodeinfo info1 = { 10 };
struct nodeinfo info2 = { 20 };
struct nodeinfo info3 = { 30 };

struct node node1 = { &info1, NULL, NULL };
struct node node2 = { &info2, NULL, NULL };
struct node node3 = { &info3, NULL, NULL };

static void swapnodes(struct node *_alpha, struct node *_beta);

int
main(void)
{
node1.n_next = &node2;
node2.n_next = &node3;
node3.n_next = &node1;
node1.n_prev = &node3;
node2.n_prev = &node1;
node3.n_prev = &node2;

swapnodes(&node1, &node2);
return (0);
}

static void
{
struct node *np;

return;
do {
if (np->n_info != NULL)
printf(" node %p -> %d\n", np,
np->n_info->ni_data);
else
printf(" node %p -> null\n", np);
np = np->n_next;
} while (np != NULL && np != head);
}

static void
swapnodes(struct node *na, struct node *nb)
{
struct nodeinfo *tmp;

assert(na != NULL && nb != NULL);
tmp = na->n_info;
na->n_info = nb->n_info;
nb->n_info = tmp;
}
------------------------------------------------------------------------------

In this case, the swapping of the two nodes is simplified to the swapping of
two pointers, the n_info members of the nodes. Note though that parts of the
program cannot keep copies of `struct node' pointers around and reuse them
later. There is no guarantee that the same nodeinfo will be linked under a
pointer to a node after a while, unless the list is locked.

> 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;
>
> [snip]

``You're lost in a maze of twisty little passages, all alike.''
-- Nethack

Your logic seems fine, and teh way you does swapped those nodes make sense,
but the names are so confusing that it took me the better part of 15 minutes
of careful reading *WITH* a notebook by my side to find out what's going on!

IMHO C source shouldn't be so complex, unless there's a very good reason.

> 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?

Sure, drawing on a whiteboard, a piece of paper or something, is certainly
going to help a lot.

- Giorgos

pete
Guest
Posts: n/a

 11-04-2004
Eugen J. Sobchenko wrote:
>
> 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?

If you swap the head of the list with another node,
then it might be simpler to return the address of the new head.

list = swap(list, list -> next);
l_print(list);
list = swap(list -> next, list);
l_print(list);
vs.
swap(list -> next, list -> next -> next);
l_print(list);

struct node *swap(struct node *a1, struct node *a2)
{
struct node *temp;

temp = a1 -> next;
a1 -> next = a2 -> next;
a2 -> next = temp;
if (a1 -> next != NULL) {
a1 -> next -> prev = a1;
}
if (a2 -> next != NULL) {
a2 -> next -> prev = a2;
}
temp = a1 -> prev;
a1 -> prev = a2 -> prev;
a2 -> prev = temp;
if (a1 -> prev != NULL) {
a1 -> prev -> next = a1;
}
if (a2 -> prev != NULL) {
a2 -> prev -> next = a2;
return a1;
} else {
return a2;
}
}

I modified the program in the below refered to post, to test it.

--
pete

Eugen J. Sobchenko
Guest
Posts: n/a

 11-05-2004
Giorgos Keramidas <(E-Mail Removed)> wrote in message news:<20041104152619.T6871@orion>...

> The constraint of keeping the references NULL or unique during the swap is
> something you cannot guarantee by reordering the assignments of the pointers.
> Some sort of locking must be used, which is machine-dependent and cannot be
> described portably here (i.e. a mutex that protects the entire list, while
> reordering operations happen).

I've omited this in my previous post. Of course I'm using mutex'es for
locking.

>
> One trick that you can use to effectively 'swap' two elements of a list is to
> decouple the list linkage from the element data, using something like this:
>
> [ cut ]
>
> static void
> swapnodes(struct node *na, struct node *nb)
> {
> struct nodeinfo *tmp;
>
> assert(na != NULL && nb != NULL);
> tmp = na->n_info;
> na->n_info = nb->n_info;
> nb->n_info = tmp;
> }

Good solution. But what should we do when
we have more then one information field ( e.g. 20 )?

> [ cut ]
> Your logic seems fine, and teh way you does swapped those nodes make sense,
> but the names are so confusing that it took me the better part of 15 minutes
> of careful reading *WITH* a notebook by my side to find out what's going on!
>
> IMHO C source shouldn't be so complex, unless there's a very good reason.

You right. My fault.

>
> > 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?

>
> Sure, drawing on a whiteboard, a piece of paper or something, is certainly
> going to help a lot.

I've tried a lot but found nothing except simply draw double linked list schemas
and move links between elements on it.

>
> - Giorgos

Eugen J. Sobchenko
Guest
Posts: n/a

 11-05-2004
> Just swap the data instead of all the pointers:
>
> void swap ( struct node *a1, struct node *a2 ) {
> int temp;
> temp = a1->i;
> a1->i = a2->i;
> a2->i = temp;
> }
>

Good. But what should I do when I have tens of informational
fields?

Eugen J. Sobchenko
Guest
Posts: n/a

 11-05-2004
> If you swap the head of the list with another node,
> then it might be simpler to return the address of the new head.
>
> list = swap(list, list -> next);
> l_print(list);
> list = swap(list -> next, list);
> l_print(list);
> vs.
> swap(list -> next, list -> next -> next);
> l_print(list);
>
>
> struct node *swap(struct node *a1, struct node *a2)
> {
> struct node *temp;
>
> temp = a1 -> next;
> a1 -> next = a2 -> next;
> a2 -> next = temp;
> if (a1 -> next != NULL) {
> a1 -> next -> prev = a1;
> }
> if (a2 -> next != NULL) {
> a2 -> next -> prev = a2;
> }
> temp = a1 -> prev;
> a1 -> prev = a2 -> prev;
> a2 -> prev = temp;
> if (a1 -> prev != NULL) {
> a1 -> prev -> next = a1;
> }
> if (a2 -> prev != NULL) {
> a2 -> prev -> next = a2;
> return a1;
> } else {
> return a2;
> }
> }
>
> I modified the program in the below refered to post, to test it.

Thanks. But I'm swapping two arbitrary elements of very long
double linked list. This one seems to be very slow for me. ;-(

Giorgos Keramidas
Guest
Posts: n/a

 11-05-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Eugen J. Sobchenko) writes:
> Giorgos Keramidas <(E-Mail Removed)> wrote
> in message news:<20041104152619.T6871@orion>...
> >
> > static void
> > swapnodes(struct node *na, struct node *nb)
> > {
> > struct nodeinfo *tmp;
> >
> > assert(na != NULL && nb != NULL);
> > tmp = na->n_info;
> > na->n_info = nb->n_info;
> > nb->n_info = tmp;
> > }

>
> Good solution. But what should we do when
> we have more then one information field ( e.g. 20 )?

You can use structure assignment with a temporary struct, instead of a
pointer, which can waste a bit of space on the stack of the program for
the allocation of an automatic struct.

You can redesign the structures, so that n_info is the *only*
information a node struct keeps, and make sure all the fields you
consider 'attributes' of the node are fields of nodeinfo.

Method Man
Guest
Posts: n/a

 11-05-2004

"Eugen J. Sobchenko" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> > Just swap the data instead of all the pointers:
> >
> > void swap ( struct node *a1, struct node *a2 ) {
> > int temp;
> > temp = a1->i;
> > a1->i = a2->i;
> > a2->i = temp;
> > }
> >

>
> Good. But what should I do when I have tens of informational
> fields?

Then I would question the design. You can allocate a struct to encapsulate
all your data, and then just swap those struct pointers. For example:

struct data {
/* your fields of data */
}

struct node {
struct data* i;
struct node* p;
struct node* n;
}

void swap ( struct node *a1, struct node *a2 ) {
struct data* temp;
temp = a1->i;
a1->i = a2->i;
a2->i = temp;
}