-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDaily_Coding_Problem-06.cpp
65 lines (60 loc) · 2.26 KB
/
Daily_Coding_Problem-06.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
#include "Daily_Coding_Problem.h"
/* MARK: - SOLUTION ✨ */
// Create a node class
struct Node {
int val;
Node* both;
Node(int x) : val(x), both(NULL) {} // Constructor
};
class XORLinkedList {
public:
Node* head; // head node of the list
Node* tail; // tail node of the list
int size; // size of the list
XORLinkedList() { // initialize the list with empty head and tail nodes and size 0
head = NULL;
tail = NULL;
size = 0;
}
void add(int element) { // this function adds a new node to the end of the list with the value of element
Node* node = new Node(element);
if (head == NULL) { // IF the list is empty THEN set the head and tail to the new node
head = node;
tail = node;
} else { // OTHERWISE set the tail's both to the XOR of the tail's both and the new node and set the new node's both to the XOR of the tail and NULL
tail->both = (Node*)((uintptr_t)(tail->both) ^ (uintptr_t)(node));
node->both = tail;
tail = node;
}
size++; // incrementa the size of the list
}
Node* get(int index) { // this function returns the node at the index of the list
if (index >= size) return NULL;
Node* prev = NULL;
Node* curr = head;
Node* next;
for (int i = 0; i < index; i++) { // for each node in the list from the head to the index node
// XOR the prev and next nodes and set the prev to the curr and
// the curr to the next
next = (Node*)((uintptr_t)(curr->both) ^ (uintptr_t)(prev)); // convert the curr's both to a uintptr_t and XOR
// it with the prev to get the next node because the curr's both is the XOR of the prev and next nodes
prev = curr;
curr = next;
}
return curr;
}
};
// MARK: - Main func
int main() {
XORLinkedList list;
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
cout << list.get(0)->val << endl;
cout << list.get(1)->val << endl;
cout << list.get(2)->val << endl;
cout << list.get(3)->val << endl;
cout << list.get(4)->val << endl;
}