User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,571 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,615 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 1368 | Replies: 12 | Solved
Reply
Join Date: Oct 2007
Posts: 6
Reputation: phoenixlsk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phoenixlsk phoenixlsk is offline Offline
Newbie Poster

xor linked list

  #1  
Oct 25th, 2007
hi i need some help.
i have to do a linked list using XOR and i don't know how to do it.
thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: xor linked list

  #2  
Oct 25th, 2007
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: phoenixlsk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phoenixlsk phoenixlsk is offline Offline
Newbie Poster

Re: xor linked list

  #3  
Oct 25th, 2007
"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 <->

"
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: phoenixlsk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phoenixlsk phoenixlsk is offline Offline
Newbie Poster

Re: xor linked list

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

  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
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: xor linked list

  #5  
Oct 25th, 2007
>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.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: xor linked list

  #6  
Oct 25th, 2007
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 blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 62
Reputation: Ptolemy is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
Ptolemy's Avatar
Ptolemy Ptolemy is offline Offline
Junior Poster in Training

Re: xor linked list

  #7  
Oct 25th, 2007
>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.
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: xor linked list

  #8  
Oct 25th, 2007
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.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Join Date: Oct 2007
Posts: 6
Reputation: phoenixlsk is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
phoenixlsk phoenixlsk is offline Offline
Newbie Poster

Re: xor linked list

  #9  
Oct 25th, 2007
[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?
Reply With Quote  
Join Date: May 2007
Location: Ireland
Posts: 1,761
Reputation: twomers will become famous soon enough twomers will become famous soon enough 
Rep Power: 6
Solved Threads: 34
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: xor linked list

  #10  
Oct 25th, 2007
Put (int*) before the error area.
Or look up some C++ casts.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:00 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC