XOR Linked List – A Memory Efficient Doubly Linked List


Linked List

The linked list is a linear data structure containing elements called nodes. Each node consists of two main components: data (payload of that node) and a pointer to the next node in the list. They are simple and efficient to use providing easy allocation and deallocation of memory.

Doubly Linked List

Doubly linked list is a special type of linked list which again consists of a basic element called nodes. Each node consists of three main components: data (payload of that node), a pointer to the previous node of the sequence and a pointer to the next node of the sequence. This bidirectional linkage allows efficient traversal in both directions.

XOR Linked List

An XOR linked list, also known as XOR doubly linked list uses bitwise XOR (^) operation to save memory as compared to a traditional doubly linked list. Each node consists of two main components: data (payload of that node) and XOR of the memory address of the previous and next nodes in the sequence.

Example

Given a doubly linked list: A - B - C - D

The XOR Representation is −

link(A) = NULL ^ address(B)
link(B) = address(A) ^ address(C)
link(C) = address(B) ^ address(D)
link(D) = address(C) ^ NULL

Forward Traversal

For the above example, let’s take the case of forward traversal i.e. left to right and see how we can get to the next node using the link stored in a node. Given a forward traversal so the address of the previous node is stored in a variable. Let us take we are at node B and want to get to node C.

address(A) ^ link(B) = address(A) ^ (address (A) ^ address(C))
		         = 0 ^ address(C)
		         = address(C)

Backward Traversal

For the above example, let’s take the case of backward traversal i.e. right to left and see how we can get to the next node using the link stored in a node. Given a backward traversal so the address of the previous node is stored in a variable. Let us take we are at node D and want to get to node C.

 NULL ^ link(D) = NULL ^ (address(C) ^ NULL)
		 = 0 ^ address(C)
		 = address(C)

Pseudocode

structure Node:
   data : integer
   link : Node
function xor(x : Node, y : Node) : Node
   return Node with address (address of x) xor (address of y)
function traverse(head : Node)
   curr : Node = head
   prev : Node = null
   next : Node
   while curr is not null
      output curr.data, " --> "
      next = xor(prev, curr.link)
      prev = curr
      curr = next
   end while
   output "nullptr"
function push(headRef : Node reference, data : integer)
   newNode : Node = create new Node
   newNode.data = data
   newNode.link = xor(headRef, null)
   if headRef is not null
      headRef.link = xor(newNode, xor(headRef.link, null))
   end if
   headRef = newNode

Example: C++ Implementation

#include <iostream>
#include <vector>
#include <cstdint>
using namespace std;

// Structure of XOR Linked list
struct Node {
   int data;
   Node *link;
};
// Dunction to calculate XOR of two node address
Node *XOR(Node *x, Node *y) {
   return (Node *)((uintptr_t)(x) ^ (uintptr_t)(y));
}
// Forward traversal
void traverse(Node *head) {
   Node *curr = head;
   Node *prev = nullptr;
   Node *next;
   while (curr != nullptr) {
      cout << curr->data << " --> ";
      next = XOR(prev, curr->link);
      prev = curr;
      curr = next;
   }
   cout << "nullptr";
}
// Function to push an element at front of XOR linked list
void push(Node *&headRef, int data) {
   Node *newNode = new Node();
   newNode->data = data;
   newNode->link = XOR(headRef, nullptr);
   if (headRef) {
      headRef->link = XOR(newNode, XOR(headRef->link, nullptr));
   }
   headRef = newNode;
}
int main() {
   vector<int> values = {1, 2, 3, 4};
   Node *head = nullptr;
   for (int i = values.size() - 1; i >= 0; i--) {
      push(head, values[i]);
   }
   traverse(head);
   return 0;
}

Output

1 --> 2 --> 3 --> 4 --> nullptr

Conclusion

In conclusion, XOR linked list is an efficient approach to implementing a doubly linked list. But it is a bit complex and not that easy to code as compared to a doubly linked list and also deletion of node is not possible. Also, many languages like Java do not support XOR linked list.

Updated on: 03-Nov-2023

843 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements