-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathSrc.cpp
74 lines (56 loc) · 1.47 KB
/
Src.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
70
71
72
73
74
#include "./Structure.hpp"
/*
* Next Node: XOR(prev, currentNode->npx);
* Prev Node: XOR(next, currentNode->npx);
*/
Node *XOR(Node *A, Node *B) {
return (Node *) ((uintptr_t)(A) ^ (uintptr_t)(B));
}
Node *returnLastNode(Node *head) {
Node *prev = nullptr, *current = head, *next;
next = XOR(prev, current->npx);
while (next) {
next = XOR(prev, current->npx);
prev = current;
current = next;
}
return prev;
}
void Insert(Node **head, int data) {
Node *newNode = new Node();
newNode->dt = data;
newNode->npx = XOR(*head, nullptr);
if (*head) {
Node *next = XOR((*head)->npx, nullptr);
(*head)->npx = XOR(newNode, next);
}
*head = newNode;
}
void printLLBackward(Node *head) {
Node *next = nullptr, *current = head, *prev;
while (current) {
cout << current->dt << " ";
prev = XOR(next, current->npx);
next = current;
current = prev;
}
return;
}
void printLLForward(Node *head) {
Node *prev = nullptr, *current = head, *next;
while (current) {
cout << current->dt << " ";
next = XOR(prev, current->npx);
prev = current;
current = next;
}
return;
}
int main() {
Node *head = nullptr;
Insert(&head, 5);
Insert(&head, 8);
Insert(&head, 2);
printLLForward(head); // nullptr <-> 2 <-> 8 <-> 5 <-> nullptr
printLLBackward(returnLastNode(head)); // nullptr <-> 5 <-> 8 <-> 2 <-> nullptr
}