hi i need some help.
i have to do a linked list using XOR and i don't know how to do it.
thanks

## Featured Replies in this Discussion

• >Doing a linked list using XOR ... I'm not certain what you mean. It's a hack to avoid wasting extra space on links. It takes advantage of the butterfly effect using XOR. If you take the XOR of two values and XOR it with either value, you get the other value: ... They're obscure, the logic is complex, and the result is far less flexible than a traditional linked list. If you're so pressed for…
Post Contains: Code

Please elaborate. A linked list I can understand. Exclusive or I can understanc. Doing a linked list using XOR ... I'm not certain what you mean.

"XOR linked lists are a curious use of the bitwise exclusive disjunction (XOR) operation, here denoted by ?, to decrease storage requirements for doubly-linked lists. An ordinary doubly-linked list stores addresses of the previous and next list items in each list node, requiring two address fields:
... A B C D E ...
<- prev <- prev <- prev <-
-> next -> next -> next ->

An XOR linked list compresses the same information into one address field by storing the bitwise XOR of the address for previous and the address for next in one field:

... A B C D E ...
<-> A?C <-> B?D <-> C?E <->

"

i found something but it has an error :"xor1001.cpp invalid conversion from `void*' to `link*' ".
and i don't really understand the program

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

/* pray that a long is the size of a pointer */
typedef unsigned long pointer;

typedef struct
{
pointer next_prev;

{
if (list != NULL)
}

{
pointer prev = 0;
while (list != NULL)
{
pointer next = prev ^ list->next_prev;
prev = (pointer)list;
}
printf("\n");
}

int main(void)
{
/* create a list */
/* add something to the front ... */
/* add something to the back ... */

/* walk from front to back */
walk_list(l1);
/* walk from back to front */
walk_list(l3);

return 0;
}``````

>Doing a linked list using XOR ... I'm not certain what you mean.
It's a hack to avoid wasting extra space on links. It takes advantage of the butterfly effect using XOR. If you take the XOR of two values and XOR it with either value, you get the other value:

``````123 XOR 456 = 435 (butterfly radix)

435 XOR 123 = 456
435 XOR 456 = 123``````

They're obscure, the logic is complex, and the result is far less flexible than a traditional linked list. If you're so pressed for space that you're willing to add this kind of complexity to the implementation, you should find a more suitable data structure.

Two good answers in one :)

First things first. Do you know how to write a regular linked list?

>i found something but it has an error :"xor1001.cpp
>invalid conversion from `void*' to `link*' ".
You're compiling C as C++. C++ doesn't support implicit pointer conversions to and from void*. You have to add casts.

>and i don't really understand the program
If you understood it, you could have written it yourself. :) Try to run through the logic and figure it out. The program is tiny and bare bones. You're not going to find a simpler implementation.

Thanks for the explanation, Ptolemy. I hadn't heard of them before. Sounds like fun to implement. I can only assume that one uses the 'boundrary conditions' (ie start and end nodes where previous and next respectively will be NULL), to extract the next and previous addresses of all the other elements. Would be a bit more painful if it was a completely cyclic list though.

I glossed over your explanation, sorry. I assumed you were explaining XOR to me :) Makes sense.

>i found something but it has an error :"xor1001.cpp
>invalid conversion from `void*' to `link*' ".
You're compiling C as C++. C++ doesn't support implicit pointer conversions to and from void*. You have to add casts.

but don't know how to use cast. can you help me?

Put (int*) before the error area.
Or look up some C++ casts.

>but don't know how to use cast. can you help me?
That's something that any C++ book or reference will tell you how to use. It would be "cast" or "type cast" in the index.

thank you
:-)

Question Answered as of 7 Years Ago by twomers and Ptolemy

Try reading some of the sticky posts that are labels Read Me: to learn good posting techniques...

class xll
{
public:
int data;
};

class xlist
{
xlist(){head = NULL;tail = NULL}
void insert(int);
};
void xlist :: insert (int ele)
{
xll *node = (xll*) malloc (sizeof(xll));
node->data = ele;
xll *temp = head,*prev = NULL;
while(temp != NULL)
{
next = (xll *) (temp->link ^ (unsigned long) prev);
prev = temp;
temp = next;
}
if(prev == NULL)