An XOR linked list is more memory efficient doubly linked list. Instead of each node holding next
and prev
fields, it holds a field named both
, which is an XOR of the next node and the previous node. Implement an XOR linked list; it has an add(element)
which adds the element to the end, and a get(index)
which returns the node at index.
Example:
Input: 1->2->3->4->5->NULL, index=2
Output: The XOR linked list contains: 1 2 3 4 5
The value of xor_ll at index 2 is 3
Approach
C++
#include <bits/stdc++.h>using namespace std;struct Node{int value;Node *next;//create a node with the valueNode(int new_value){this->value = new_value;this->next = NULL;}static Node *XOR(Node *pervious, Node *next){return (Node *)((uintptr_t)(pervious) ^ (uintptr_t)(next));}};class XOR_LL{public:Node *head = NULL;Node *tail = NULL;int length = 0;XOR_LL(Node *head_node){head = head_node;tail = head_node;length += 1;}void add(int value){Node *new_node = new Node(value);new_node->next = Node::XOR(tail, NULL);if (tail != NULL){Node *next = Node::XOR(tail->next, NULL);tail->next = Node::XOR(new_node, next);}length += 1;tail = new_node;}Node *get(int index){Node *curr = head;Node *prev = NULL;Node *next = NULL;for (int i = 0; i < index; i++){next = Node::XOR(prev, curr->next);prev = curr;curr = next;}return curr;}void print_list(){Node *curr = head;Node *prev = NULL;Node *next = NULL;std::cout << "The XOR linked list contains: ";while (curr != NULL){std::cout << curr->value << " ";next = Node::XOR(prev, curr->next);prev = curr;curr = next;}std::cout << "\n";}};int main(){// Create the XOR linked listNode *head = new Node(1);XOR_LL xor_ll(head);xor_ll.add(2);xor_ll.add(3);xor_ll.add(4);xor_ll.add(5);// Print the listxor_ll.print_list();// Get the node at the index// Indexing starts at 0int index = 2;Node *indexed_node = xor_ll.get(index);cout << "The value of xor_ll at index "<< index << " is " << indexed_node->value << "\n";return 0;}
No comments:
Post a Comment