forked from AdarshAddee/Hacktoberfest2022_for_Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XOR LL.cpp
68 lines (50 loc) 路 1 KB
/
XOR LL.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <bits/stdc++.h>
#include <cinttypes>
using namespace std;
class Node
{
public:
int data;
Node *xnode;
};
Node *Xor(Node *x, Node *y)
{
return reinterpret_cast<Node *>(
reinterpret_cast<uintptr_t>(x) ^ reinterpret_cast<uintptr_t>(y));
}
void insert(Node **head_ref, int data)
{
Node *new_node = new Node();
new_node->data = data;
new_node->xnode = *head_ref;
if (*head_ref != NULL)
{
(*head_ref)
->xnode = Xor(new_node, (*head_ref)->xnode);
}
*head_ref = new_node;
}
void printList(Node *head)
{
Node *curr = head;
Node *prev = NULL;
Node *next;
cout << "The nodes of Linked List are: \n";
while (curr != NULL)
{
cout << curr->data << " ";
next = Xor(prev, curr->xnode);
prev = curr;
curr = next;
}
}
int main()
{
Node *head = NULL;
insert(&head, 10);
insert(&head, 100);
insert(&head, 1000);
insert(&head, 10000);
printList(head);
return (0);
}