Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. To insert a node at the back of a XOR doubly linked list

To insert a node at the back of a XOR doubly linked list

Scheduled Pinned Locked Moved ATL / WTL / STL
data-structuresperformanceannouncement
3 Posts 3 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    here is the method to insert a node at the front in the XORed doubly linked list or also known as memory efficient doubly linked list :

    // To write a memory efficient linked list
    #include using namespace std;

    // Node structure of a memory efficient doubly linked list
    class Node {
    public:
    int data;
    Node *npx; // XOR of next and previous node
    };

    // return XORed value of the node address
    Node *XOR(Node *a, Node *b){
    return ((Node *)( (uintptr_t)(a) ^ (uintptr_t)(b) ));
    }

    // insert a node at the beggining of the XORed linked list and makes the newly inserted node as head
    void insert(Node **head_ref, int data){
    // allocate memory for new node
    Node *new_node = new Node();
    new_node->data = data;

    // since node is inserted at the beggining, npx of the new node will always be XOR of curent head and null
    new\_node->npx = XOR((\*head\_ref), nullptr);
    
    // if the linked list is not empty, then npx of current head node will be XOR of new node and node next to current head
    if(\*head\_ref != nullptr){
        // (\*head\_ref)->npx is XOR of null and next.
        // so if we do XOR of it with null, we get next
        Node \*next = XOR((\*head\_ref)->npx, nullptr);
        (\*head\_ref)->npx = XOR(new\_node, next);
    }
    
    // change head
    \*head\_ref = new\_node;
    

    }

    // prints contents of doubly linked list in forward direction
    void printList(Node *head){
    Node *curr = head, *prev = nullptr, *next;

    cout << "Following are the nodes of Linked List: \\n";
    
    while(curr != nullptr) {
        // print current node
        cout << curr->data << " ";
    
        // get the address of next node : curr->npa is next^prev,
        // so curr->npx^prev will be next^prev^prev which is next
        next = XOR(prev, curr->npx);
    
        // update prev and curr for next iteration
        prev = curr;
        curr = next;
    }
    

    }

    // Driver function
    int main(){
    Node *head = nullptr;
    insert(&head, 10);
    insert(&head, 20);
    insert(&head, 30);
    insert(&head, 40);
    insert(&head, 50);
    insert(&head, 60);

    printList(head);
    
    return 0;
    

    }

    Here is the code i have tried to put an element at the back of the list:

    Node *insert(Node **last, int data){
    // allocate memory for new node
    Node *new_node = new Node();
    new_node->data = data;

    new\_node->npx = XOR(\*last, nullptr);
    
    if(\*last != nullptr) {
    	Node \*prev = XOR((\*last)->npx, nullptr)
    
    Z 1 Reply Last reply
    0
    • T Tarun Jha

      here is the method to insert a node at the front in the XORed doubly linked list or also known as memory efficient doubly linked list :

      // To write a memory efficient linked list
      #include using namespace std;

      // Node structure of a memory efficient doubly linked list
      class Node {
      public:
      int data;
      Node *npx; // XOR of next and previous node
      };

      // return XORed value of the node address
      Node *XOR(Node *a, Node *b){
      return ((Node *)( (uintptr_t)(a) ^ (uintptr_t)(b) ));
      }

      // insert a node at the beggining of the XORed linked list and makes the newly inserted node as head
      void insert(Node **head_ref, int data){
      // allocate memory for new node
      Node *new_node = new Node();
      new_node->data = data;

      // since node is inserted at the beggining, npx of the new node will always be XOR of curent head and null
      new\_node->npx = XOR((\*head\_ref), nullptr);
      
      // if the linked list is not empty, then npx of current head node will be XOR of new node and node next to current head
      if(\*head\_ref != nullptr){
          // (\*head\_ref)->npx is XOR of null and next.
          // so if we do XOR of it with null, we get next
          Node \*next = XOR((\*head\_ref)->npx, nullptr);
          (\*head\_ref)->npx = XOR(new\_node, next);
      }
      
      // change head
      \*head\_ref = new\_node;
      

      }

      // prints contents of doubly linked list in forward direction
      void printList(Node *head){
      Node *curr = head, *prev = nullptr, *next;

      cout << "Following are the nodes of Linked List: \\n";
      
      while(curr != nullptr) {
          // print current node
          cout << curr->data << " ";
      
          // get the address of next node : curr->npa is next^prev,
          // so curr->npx^prev will be next^prev^prev which is next
          next = XOR(prev, curr->npx);
      
          // update prev and curr for next iteration
          prev = curr;
          curr = next;
      }
      

      }

      // Driver function
      int main(){
      Node *head = nullptr;
      insert(&head, 10);
      insert(&head, 20);
      insert(&head, 30);
      insert(&head, 40);
      insert(&head, 50);
      insert(&head, 60);

      printList(head);
      
      return 0;
      

      }

      Here is the code i have tried to put an element at the back of the list:

      Node *insert(Node **last, int data){
      // allocate memory for new node
      Node *new_node = new Node();
      new_node->data = data;

      new\_node->npx = XOR(\*last, nullptr);
      
      if(\*last != nullptr) {
      	Node \*prev = XOR((\*last)->npx, nullptr)
      
      Z Offline
      Z Offline
      ZurdoDev
      wrote on last edited by
      #2

      Tarun Jha wrote:

      But it is not working

      Debug it and find out what is happening.

      Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

      J 1 Reply Last reply
      0
      • Z ZurdoDev

        Tarun Jha wrote:

        But it is not working

        Debug it and find out what is happening.

        Social Media - A platform that makes it easier for the crazies to find each other. Everyone is born right handed. Only the strongest overcome it. Fight for left-handed rights and hand equality.

        J Offline
        J Offline
        Jacky Joy
        wrote on last edited by
        #3

        thanks for the awesome information. https://creditcardsupportx.com/lord-and-taylor-credit-card https://creditcardsupportx.com/pep-boys-credit-card https://creditcardsupportx.com/bobs-furniture-credit-card

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups