| | |
xor linked list
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
"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 <->
"
... 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 <->
"
•
•
Join Date: Oct 2007
Posts: 6
Reputation:
Solved Threads: 0
i found something but it has an error :"xor1001.cpp invalid conversion from `void*' to `link*' ".
and i don't really understand the program
and i don't really understand the program
C++ Syntax (Toggle Plain Text)
#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; int payload; } link; link* add_data(int payload, link* list) { link *new_link = malloc(sizeof(link)); assert(new_link); new_link->next_prev = (pointer)list; new_link->payload = payload; if (list != NULL) list->next_prev ^= (pointer)new_link; return new_link; } void walk_list(link *list) { pointer prev = 0; while (list != NULL) { pointer next = prev ^ list->next_prev; printf("%d ", list->payload); prev = (pointer)list; list = (link*)next; } printf("\n"); } int main(void) { /* create a list */ link *l2 = add_data(2, NULL); /* add something to the front ... */ link *l1 = add_data(1, l2); /* add something to the back ... */ link *l3 = add_data(3, l2); /* walk from front to back */ walk_list(l1); /* walk from back to front */ walk_list(l3); return 0; }
Last edited by Ancient Dragon; Oct 25th, 2007 at 6:12 pm. Reason: add code tags
>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 space that you're willing to add this kind of complexity to the implementation, you should find a more suitable data structure.
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:
C++ Syntax (Toggle Plain Text)
123 XOR 456 = 435 (butterfly radix) 435 XOR 123 = 456 435 XOR 456 = 123
First things first. Do you know how to write a regular linked list?
Last edited by twomers; Oct 25th, 2007 at 3:33 pm.
>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.
>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 glossed over your explanation, sorry. I assumed you were explaining XOR to me
Makes sense. Last edited by twomers; Oct 25th, 2007 at 3:35 pm.
![]() |
Similar Threads
- Removing an item from head of linked list (C)
- help implementing singly linked list (C++)
- How to read in a sentence and insert in to linked list? (C++)
- Linked List using pointers (C++ ADT) (C++)
- Why doesn't this remove the last node in a linked list? (C++)
- Cannot figure out how to implement linked list and rbtree for a project! (Java)
- Linked List (C++)
- help by sorting a simply linked list (C)
Other Threads in the C++ Forum
- Previous Thread: Dice rolling program... Output trouble
- Next Thread: I can't use one member function in another member function
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






