这是一个数据结构。利用计算机的的位异或操作(⊕),来降低双向链表的存储需求。

...  A       B         C         D         E  ...
–> next –> next –> next –>
<– prev <– prev <– prev <–
双向链表如上面所示,每个节点有两个指针,分别指向该节点的前驱和后继。
而XOR链表如下面所示: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 <->
 link(B) = addr(A)⊕addr(C), link(C) = addr(B)⊕addr(D), ..
要求addr(D),使用:addr(D) = link(C) ⊕ addr(B)
地址指针中存放的地址的异或。 比如B中就存放了A⊕C的值。
这样从头开始便利时,我们需要知道开始的两个节点(而不像普通链表只需要一个)。
这样比如知道了节点A和节点B 我们就可以用节点A的地址和B中的A⊕C 结合运算,得到A⊕A⊕C 得到C。 这是正向遍历
反向遍历时类似,比如知道了节点E和D,就可以利用E的地址和D中的C⊕E 运算,得到C⊕E⊕E 得到C.

To start traversing the list in either direction from some point, you need the address of two consecutive items, not just one. If the addresses of the two consecutive items are reversed, you will end up traversing the list in the opposite direction.

This form of linked list may be inadvisable: 异或链表不推荐使用:

  • General-purpose debugging tools cannot follow the XOR chain, making debugging more difficult; [1]
  • The price for the decrease in memory usage is an increase in code complexity, making maintenance more expensive; 虽然内存使用减少了,但代码复杂量增加了,使维护更加昂贵。
  • Most garbage collection schemes do not work with data structures that do not contain literal pointers;通常的垃圾回收机制对于这种表示无法正常工作
  • XOR of pointers is not defined in some contexts (e.g., the C language), although many languages provide some kind of type conversion between pointers and integers;
  • The pointers will be unreadable if one isn't traversing the list — for example, if the pointer to a list item was contained in another data structure;不是在遍历链表时,指针是无意义的。例如另外的数据结构也包含了链表中的指针。
  • While traversing the list you need to remember the address of the previously accessed node in order to calculate the next node's address.
  • XOR linked lists do not provide some of the important advantages of doubly-linked lists, such as the ability to delete a node from the list knowing only its address or the ability to insert a new node before or after an existing node when knowing only the address of the existing node.XOR链表也无法提供双向链表提供的一些功能。比如仅知道一个节点是将该节点从链表移除或者在仅知道一个节点时再该节点后面插入一个节点。

Computer systems have increasingly cheap and plentiful memory, and storage overhead is not generally an overriding issue outside specialized embedded systems. Where it is still desirable to reduce the overhead of a linked list, unrolling provides a more practical approach (as well as other advantages, such as increasing cache performance and speeding random access).

实现代码:http://blog.wsensors.com/2011/04/the-xor-linked-list-in-c/

类似的变形

加法链表:放地址的和

 ...  A        B         C         D         E  ...
<–> A+C <-> B+D <-> C+E <->
减法链表:放地址的差
...  A        B         C         D         E  ...
<–> C-A <-> D-B <-> E-C <->
不同是正向和反向需要做不同的运算。

参考:http://en.wikipedia.org/wiki/XOR_linked_list

XOR双向链表的更多相关文章

  1. 异或链表(XOR linked list)

    异或链表(Xor Linked List)也是一种链式存储结构,它可以降低空间复杂度达到和双向链表一样目的,任何一个节点可以方便的访问它的前驱节点和后继结点.可以参阅wiki 普通的双向链表 clas ...

  2. CLRS10.2-8练习 - 单指针值实现双向链表

    要求: Explain how to implement doubly linked lists using only one pointer value x.np peritem instead o ...

  3. XOR linked list--- 异或链表

    异或链表的结构 这是一个数据结构.利用计算机的的位异或操作(⊕),来降低双向链表的存储需求. ... A B C D E ... –> next –> next –> next –& ...

  4. 学习Redis你必须了解的数据结构——双向链表(JavaScript实现)

    本文版权归博客园和作者吴双本人共同所有,转载和爬虫请注明原文链接 http://www.cnblogs.com/tdws/ 下午分享了JavaScript实现单向链表,晚上就来补充下双向链表吧.对链表 ...

  5. [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字

    Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...

  6. 二分+DP+Trie HDOJ 5715 XOR 游戏

    题目链接 XOR 游戏 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  7. 双向链表、双向循环链表的JS实现

    关于链表简介.单链表.单向循环链表.JS中的使用以及扩充方法:  单链表.循环链表的JS实现 关于四种链表的完整封装: https://github.com/zhuwq585/Data-Structu ...

  8. 剑指Offer面试题:25.二叉搜索树与双向链表

    一.题目:二叉搜索树与双向链表 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向.比如输入下图中左边的二叉搜索树,则输出转换之后的 ...

  9. BZOJ 2115 【Wc2011】 Xor

    Description Input 第一行包含两个整数N和 M, 表示该无向图中点的数目与边的数目. 接下来M 行描述 M 条边,每行三个整数Si,Ti ,Di,表示 Si 与Ti之间存在 一条权值为 ...

随机推荐

  1. 初学Django

    纵然有众多大牛写过这些简单入门文章,但作为记录,还是要自己动手写下来的比较靠谱,‘好脑筋不如烂笔头’啊! Python 安装 Django本身是纯Python编写的,所以安装框架的第一步是确保你已经安 ...

  2. SQLite header and source version mismatch 解决方案

    我下载了sqlite源码,并且configure, make, make install. 然后就出现SQLite header and source version mismatch  的错误. 上 ...

  3. WPF中图形表示语法详解(Path之Data属性语法)

    原文 http://blog.csdn.net/johnsuna/article/details/1885597 老规矩,看图说话. 先看显示效果:(图1) XAML(代码A):<Page xm ...

  4. [转]Net Framework引路蜂地图开发示例

    From:http://www.2cto.com/kf/201207/140421.html 引路蜂地图也提供对.Net Framework平台的支持,可以开发桌面地图应用,由于Mono C#的跨平台 ...

  5. Hibernate 、多表关联映射-组件关联映射(component)

    组件关联映射可以将一些简小的数据与主题放在一个表中,例如firstName 和LastName这两个结合在一起可以组成一个名字,但是再分别将这两个再建一个表就不太合适了,这个时候可以用到组件关联映射: ...

  6. 合理的keyword密度散布与黑帽SEO之躲藏文本

    合理的keyword密度散布与黑帽SEO之躲藏文本 咱们都晓得.关于baidu的keyword排行有一个非常重要的条件即是keyword密度.在咱们的了解中keyword的密度在2%-8%这个规模之内 ...

  7. o​r​a​l​c​e​ ​D​B​A​ ​培​训_lesson06

    控制文件 -小型二进制文件 -定义物理数据库的当前状态 -丢失控制文件须要修复 -维护数据库的完整性 -要求: 1.在启动数据库时处于mount状态 2.可以操作数据库 -仅仅链接至一个数据库 -最初 ...

  8. android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds差别

    手工设置文本与图片相对位置时.经常使用到例如以下方法: setCompoundDrawables(left, top, right, bottom) setCompoundDrawablesWithI ...

  9. webview与JavaScript之间的交互

    据说WebView的强大之处就是能和JavaScript进行交互调用. 参考博客:http://droidyue.com/blog/2014/09/20/interaction-between-jav ...

  10. iOS - SQLite Database 操作数据库

    iOS - SQLite Database 操作数据库   Sqlite 能被用在ios上做数据处理用,只要你懂得一点sql 就很容易使用sqlite 1:创建一个简单的View based appl ...