Convert Singly Linked List To Xor Linked List In C++

In this article, we will discuss how to convert Singly Linked List to XOR Linked List in C++. Before going to its implementation, we will know about the Singly Linked List and XOR Linked List.

What are Singly Linked Lists?

Singly linked lists are a kind of linked list in which every node points to the following node in the sequence. There is not a pointer in icpp tutorialing to the node before it. This implies that the list can only be accessed in a forward direction.

A pointer named head points to the first item in the list, and a tail pointer points to the last item in the list.

Operations on Singly Listed Lists are:

  • Items can be added to the head.
  • Items can be inserted at the end.
  • Searching for an element in the list.
  • Insert one element after another element.
  • Delete the item at the end.
  • Delete the item anywhere in the list.
  • Items can be removed from the head.
  • What are XOR Linked Lists?

The memory-efficient variant of the doubly linked list is called an XOR linked list, where each node's nexcpp tutorialer holds the XOR of its address from the previous and next nodes. The addresses of the previous and next nodes in an XOR linked list are stored in just one address space per node, but a doubly linked list stores the addresses of the previous and next list nodes (prev, next) in two address spaces per node.

Method for Creating an XOR linked list from a singly linked list:

  • An XOR linked list contains the XOR of the previous and next node addresses in each pointer.
  • Initially, we iterate through the singly linked list, which maintains a pointer to the preceding node.
  • We will modify each node's nexcpp tutorialer as we traverse the list by using next = XOR(prev, next).
  • Algorithm for creating a XOR linked list from a Singly Linked List

  • First, we need to create 3 nodes.
  • A curr node that first directs towards the head.
  • A prev node thacpp tutorialed to the null at first.
  • The next node pointed to the curr at first.
  • Repeat as long as the curr is not null:
  • Update next to curr.next.
  • Update curr.next to XOR(prev, next).
  • Update the prev to the curr.
  • Update curr to the next.
  • displayingXOR
  • Printing XOR:

  • We will begin the PrintingXOR function with a node prev thacpp tutorials to NULL and a node curr thacpp tutorials to the head.
  • We are about to print the curr data.
  • Now, we need the address of the subsequent node for the traversal.
  • The calculation is as follows: address (prev node) XOR address (curr.next).
  • We will update our curr to the next node address and our prev to the curr after obtaining the next node address.
  • This whole process continues until the end of the list.
  • Example:

Let us take an example to convert Singly Linked List to XOR Linked List in C++.

Example

#include <iostream>
#include <cstdint>
using namespace std;
// Node structure for the XOR linked list
struct Node {
    int data;
    uintptr_t xorPtr; // Pointer that holds XOR of next and previous node addresses
};
class XORLinkedList {
private:
    Node *head;
public:
    XORLinkedList() : head(nullptr) {}
    void insert(int data) {
        Node *newNode = new Node;
        newNode->data = data;
        newNode->xorPtr = reinterpret_cast<uintptr_t>(nullptr);
        if (head == nullptr) {
            head = newNode;
        } else {
            newNode->xorPtr = reinterpret_cast<uintptr_t>(head);
            head->xorPtr ^= reinterpret_cast<uintptr_t>(newNode);
            head = newNode;
        }
    }
    // Function to traverse the XOR linked list in the forward direction
    void traverseForward() {
        Node *prev = nullptr;
        Node *current = head;
        Node *next;
        while (current != nullptr) {
            cout << current->data << " ";
            // Move to the next node
            next = reinterpret_cast<Node *>(reinterpret_cast<uintptr_t>(prev) ^ current->xorPtr);
            // Update prev and currencpp tutorialers
            prev = current;
            current = next;
        }
        cout << endl;
    }
};
int main() {
    XORLinkedList xorList;
    // Insert elements into the list until the user decides to stop
    int data;
    char choice;
    do {
        cout << "Enter data to insert: ";
        cin >> data;
        xorList.insert(data);
        cout << "If you want to insert element press 'y' else 'n' ";
        cin >> choice;
    } while (choice == 'y' || choice == 'Y');
    // Traverse the list
    cout << "XOR Linked List: ";
    xorList.traverseForward();
    return 0;
}

Output:

Explanation:

  • In this example, the data and an XOR of the addresses of the previous and next nodes are stored in the Node structure.
  • After that, the XORLinkedList class has methods for traversing the list forward and inserting nodes at the start.
  • In order to preserve the XOR property, the insert method uses the XOR operation.
  • In order to get the next node address, the traverseForward method iterates across the list using XOR logic.
  • Time Complexity:

  • It is O(n) because we are traversing only the list.

Input Required

This code uses input(). Please provide values below: