#include <iostream>
class Node {
public:
int data;
Node* both;
Node(int data) : data(data), both(nullptr) {}
};
class XORLinkedList {
private:
Node* head;
public:
XORLinkedList() : head(nullptr) {}
Node XOR(Node a, Node* b) {
return reinterpret_cast<Node*>(reinterpret_cast<uintptr_t>(a) ^ reinterpret_cast<uintptr_t>(b));
}
void insert(int data) {
Node* new_node = new Node(data);
if (!head) {
head = new_node;
} else {
Node* current = head;
Node* prev = nullptr;
Node* next = XOR(nullptr, current->both);
while (next) {
prev = current;
current = next;
next = XOR(prev, current->both);
}
current->both = XOR(prev, new_node);
new_node->both = XOR(current, nullptr);
}
}
void display() {
Node* current = head;
Node* prev = nullptr;
Node* next;
while (current) {
std::cout << current->data << " ";
next = XOR(prev, current->both);
prev = current;
current = next;
}
std::cout << std::endl;
}
};
int main() {
XORLinkedList xorList;
xorList.insert(1);
xorList.insert(2);
xorList.insert(3);
xorList.display();
return 0;
}