943,683 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4404
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 25th, 2007
0

xor linked list

Expand Post »
hi i need some help.
i have to do a linked list using XOR and i don't know how to do it.
thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phoenixlsk is offline Offline
6 posts
since Oct 2007
Oct 25th, 2007
0

Re: xor linked list

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 25th, 2007
0

Re: xor linked list

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

"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phoenixlsk is offline Offline
6 posts
since Oct 2007
Oct 25th, 2007
0

Re: xor linked list

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

C++ Syntax (Toggle Plain Text)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <assert.h>
  4.  
  5. /* pray that a long is the size of a pointer */
  6. typedef unsigned long pointer;
  7.  
  8. typedef struct
  9. {
  10. pointer next_prev;
  11. int payload;
  12. } link;
  13.  
  14. link* add_data(int payload, link* list)
  15. {
  16. link *new_link = malloc(sizeof(link));
  17. assert(new_link);
  18. new_link->next_prev = (pointer)list;
  19. new_link->payload = payload;
  20. if (list != NULL)
  21. list->next_prev ^= (pointer)new_link;
  22. return new_link;
  23. }
  24.  
  25. void walk_list(link *list)
  26. {
  27. pointer prev = 0;
  28. while (list != NULL)
  29. {
  30. pointer next = prev ^ list->next_prev;
  31. printf("%d ", list->payload);
  32. prev = (pointer)list;
  33. list = (link*)next;
  34. }
  35. printf("\n");
  36. }
  37.  
  38. int main(void)
  39. {
  40. /* create a list */
  41. link *l2 = add_data(2, NULL);
  42. /* add something to the front ... */
  43. link *l1 = add_data(1, l2);
  44. /* add something to the back ... */
  45. link *l3 = add_data(3, l2);
  46.  
  47. /* walk from front to back */
  48. walk_list(l1);
  49. /* walk from back to front */
  50. walk_list(l3);
  51.  
  52. return 0;
  53. }
Last edited by Ancient Dragon; Oct 25th, 2007 at 6:12 pm. Reason: add code tags
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phoenixlsk is offline Offline
6 posts
since Oct 2007
Oct 25th, 2007
1

Re: xor linked list

>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:
C++ Syntax (Toggle Plain Text)
  1. 123 XOR 456 = 435 (butterfly radix)
  2.  
  3. 435 XOR 123 = 456
  4. 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.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: xor linked list

First things first. Do you know how to write a regular linked list?
Last edited by twomers; Oct 25th, 2007 at 3:33 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 25th, 2007
0

Re: xor 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.
Reputation Points: 44
Solved Threads: 8
Junior Poster in Training
Ptolemy is offline Offline
62 posts
since Oct 2007
Oct 25th, 2007
0

Re: xor linked list

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.
Last edited by twomers; Oct 25th, 2007 at 3:35 pm.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Oct 25th, 2007
0

Re: xor linked list

[QUOTE=Ptolemy;457374]>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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
phoenixlsk is offline Offline
6 posts
since Oct 2007
Oct 25th, 2007
0

Re: xor linked list

Put (int*) before the error area.
Or look up some C++ casts.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Dice rolling program... Output trouble
Next Thread in C++ Forum Timeline: I can't use one member function in another member function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC