XOR Linked List – A Memory Efficient Doubly Linked List
来源:互联网 发布:最优化文章 编辑:程序博客网 时间:2024/06/05 04:28
An ordinary Doubly Linked List requires space for two address fields to store the addresses of previous and next nodes. A memory efficient version of Doubly Linked List can be created using only one space for address field with every node. This memory efficient Doubly Linked List is called XOR Linked List or Memory Efficient as the list uses bitwise XOR operation to save space for one address. In the XOR linked list, instead of storing actual memory addresses, every node stores the XOR of addresses of previous and next nodes.
Consider the above Doubly Linked List. Following are the Ordinary and XOR (or Memory Effiecient) representations of the Doubly Linked List.
Ordinary Representation:
Node A:
prev = NULL, next = add(B) // previous is NULL and next is address of B
Node B:
prev = add(A), next = add(C) // previous is address of A and next is address of C
Node C:
prev = add(B), next = add(D) // previous is address of B and next is address of D
Node D:
prev = add(C), next = NULL // previous is address of C and next is NULL
XOR List Representation:
Let us call the address variable in XOR representation npx (XOR of next and previous)
Node A:
npx = 0 XOR add(B) // bitwise XOR of zero and address of B
Node B:
npx = add(A) XOR add(C) // bitwise XOR of address of A and address of C
Node C:
npx = add(B) XOR add(D) // bitwise XOR of address of B and address of D
Node D:
npx = add(C) XOR 0 // bitwise XOR of address of C and 0
Traversal of XOR Linked List:
We can traverse the XOR list in both forward and reverse direction. While traversing the list we need to remember the address of the previously accessed node in order to calculate the next node’s address. For example when we are at node C, we must have address of B. XOR of add(B) and npx of C gives us the add(D). The reason is simple: npx(C) is “add(B) XOR add(D)”. If we do xor of npx(C) with add(B), we get the result as “add(B) XOR add(D) XOR add(B)” which is “add(D) XOR 0″ which is “add(D)”. So we have the address of next node. Similarly we can traverse the list in backward direction.
We have covered more on XOR Linked List in the following post.
XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
References:
http://en.wikipedia.org/wiki/XOR_linked_list
http://www.linuxjournal.com/article/6828?page=0,0
- XOR Linked List – A Memory Efficient Doubly Linked List
- (转)A Memory-Efficient Doubly Linked List
- A Memory-Efficient Doubly Linked List
- Doubly linked list
- Doubly Linked List
- HWK:A doubly linked list using class
- 【Hackerrank】Reverse a doubly linked list
- Linked List/Singly-Linked List/Doubly-Linked List
- 【Hackerrank】Insert a node into a sorted doubly linked list
- XOR Linked List
- Reverse a doubly linked list in O(1) time
- 逆置双向链表 Reverse a doubly linked list
- [linkedin] flatten a doubly multi level linked list
- Convert a Binary Tree to Doubly Linked List
- Linked List {singly linked list -> doubly linked list -> circular linked list}
- Convert BST to Sorted Doubly-Linked List
- 双向链表(Doubly Linked List)
- 链表ALDS1-3-C:Doubly Linked List
- ZOJ - 3623 Battle Ships(完全背包或者BFS)
- IOS类与类之间的传值
- sql 查询结果增加一列,列名固定,值也固定(mysql可行,其他未知)
- TeamForge使用笔记3-配置管理
- LintCode --number-of-airplanes-in-the-sky(数飞机)
- XOR Linked List – A Memory Efficient Doubly Linked List
- 同步通信和异步通信
- ESP8266 Linux开发环境搭建
- 使用uc/os开发要明确的几个堆栈
- 天声人語 20150919
- java设计模式
- 新书《Jfinal极速开发实战》正式发布
- MySQL会出现中文乱码
- 日经春秋 20150919