-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxor_linkedlist.cpp
69 lines (60 loc) · 1.04 KB
/
xor_linkedlist.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
69
#include <bits/stdc++.h>
#include<inttypes.h>
using namespace std;
struct Node
{
int data;
Node* both;
};
Node* XOR(Node* a, Node* b)
{
return (Node*)((uintptr_t)(a) ^ (uintptr_t)(b));
}
class LinkedList{
public:
Node* head;
LinkedList(int value){
head->data = value;
head->both = NULL;
}
void addNode(int value){
Node* newNode = new Node;
newNode->data = value;
Node* prev = NULL;
Node* curr = head;
Node* next = NULL;
while (curr != NULL)
{
next = XOR(prev, curr->both);
prev = curr;
curr = next;
}
prev->both = XOR(prev->both, newNode);
newNode->both = XOR(NULL, prev);
return ;
}
void display()
{
Node* prev = NULL;
Node* curr = head;
Node* next;
while(curr != NULL)
{
cout << curr->data <<" "<<curr->both<<endl;
next = XOR(prev, curr->both);
prev = curr;
curr = next;
}
}
};
int main ()
{
LinkedList list(1);
list.addNode(2);
list.addNode(3);
list.addNode(4);
list.display();
//cout << list.head->data << " " << list.head->both << endl;
//Node* temp = list.head;
return 0;
}