#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
struct
Node {
int
data;
struct
Node* nxp;
};
struct
Node* XOR(
struct
Node* a,
struct
Node* b)
{
return
(
struct
Node*)((
uintptr_t
)(a) ^ (
uintptr_t
)(b));
}
struct
Node* insert(
struct
Node** head,
int
value)
{
if
(*head == NULL) {
struct
Node* node
= (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
node->data = value;
node->nxp = XOR(NULL, NULL);
*head = node;
}
else
{
struct
Node* curr = *head;
struct
Node* prev = NULL;
struct
Node* node
= (
struct
Node*)
malloc
(
sizeof
(
struct
Node));
curr->nxp = XOR(node, XOR(NULL, curr->nxp));
node->nxp = XOR(NULL, curr);
*head = node;
node->data = value;
}
return
*head;
}
void
printList(
struct
Node** head)
{
struct
Node* curr = *head;
struct
Node* prev = NULL;
struct
Node* next;
while
(curr != NULL) {
printf
(
"%d "
, curr->data);
next = XOR(prev, curr->nxp);
prev = curr;
curr = next;
}
printf
(
"\n"
);
}
struct
Node* reverse(
struct
Node** head)
{
struct
Node* curr = *head;
if
(curr == NULL)
return
NULL;
else
{
struct
Node* prev = NULL;
struct
Node* next;
while
(XOR(prev, curr->nxp) != NULL) {
next = XOR(prev, curr->nxp);
prev = curr;
curr = next;
}
*head = curr;
return
*head;
}
}
int
main()
{
struct
Node* head = NULL;
insert(&head, 10);
insert(&head, 20);
insert(&head, 30);
insert(&head, 40);
printf
(
"XOR linked list: "
);
printList(&head);
reverse(&head);
printf
(
"Reversed XOR linked list: "
);
printList(&head);
return
(0);
}