# 异或链表

## 概述

``` ...  A       B         C         D         E  ...
–>  next –>  next  –>  next  –>
<–  prev <–  prev  <–  prev  <–
```

``` ...  A         B               C                D            E  ...
<–>  A⊕C     <->    B⊕D     <->      C⊕E  <->

link(B) = addr(A)⊕addr(C), link(C) = addr(B)⊕addr(D), …
```

## C语言示例（异或指针双向链表）

### 异或链表结点结构

```typedef struct XorNode {
char data;
struct XorNode *LRPtr;
}
XorNode, *XorPointer;
```

### 异或指针双向链表结构

```typedef struct XorLinkedList {
XorPointer left, right;
}
XorLinkedList;
```

### 异或操作

```XorPointer xor(XorPointer a, XorPointer b) {
return (XorPointer)((unsigned long)(a) ^ (unsigned long)(b));
}
```

### 创建异或双向链表

```void createXorLinkedList(XorLinkedList *list) {
char ch;
XorPointer lastNode = NULL;
int isFirstNode = 1;
while ((ch = getchar()) != '\n') {
XorPointer newNode = (XorPointer) malloc(sizeof(XorNode));
newNode -> data = ch;
newNode -> LRPtr = NULL;
if (lastNode) {
newNode -> LRPtr = xor(lastNode, NULL);
lastNode -> LRPtr = xor(xor(lastNode -> LRPtr, NULL), newNode);
}
lastNode = newNode;
if (isFirstNode) {
isFirstNode = 0;
list -> left = newNode;
}
}
list -> right = lastNode;
}
```

### 按任意方向依次输出各结点值

```void print(XorPointer a, XorPointer b) {
XorPointer nullFirst = a == NULL ? a : b;
XorPointer nonNullFirst = a != NULL ? a : b;
XorPointer tmp = NULL;
do {
printf("%c ", nonNullFirst -> data);
tmp = nonNullFirst;
nonNullFirst = xor(nullFirst, nonNullFirst -> LRPtr);
nullFirst = tmp;
} while (nonNullFirst != NULL);
}
```

### 在第i个结点之前插入一个结点

```void XorLinkedListInsert(XorLinkedList *list, int pos, char data) {
XorPointer node = list -> left, tmp = NULL;
XorPointer last = NULL;
XorPointer newNode = NULL;
int i = 1;
while (i < pos && node != NULL) {
tmp = node;
node = xor(last, node -> LRPtr);
last = tmp;
i++;
}
newNode = (XorPointer) malloc(sizeof(XorNode));
newNode -> data = data;
newNode -> LRPtr = xor(last, node);
if (node != NULL) {
node -> LRPtr = xor(newNode, xor(last, node -> LRPtr));
}
if (last != NULL) {
last -> LRPtr = xor(xor(last -> LRPtr, node), newNode);
}
if (pos == 1) {
list -> left = newNode;
}
}
```

### 删除第i个结点

```void XorLinkedListDelete(XorLinkedList *list, int pos) {
XorPointer node = list -> left, last = NULL, next = NULL, tmp = NULL;
int i = 1;
while (i < pos && node != NULL) {
i++;
tmp = node;
node = xor(last, node -> LRPtr);
last = tmp;
}
next = xor(last, node -> LRPtr);
if (last != NULL) {
last -> LRPtr = xor(xor(last -> LRPtr, node), next);
}
if (next != NULL) {
next -> LRPtr = xor(last, xor(node, next -> LRPtr));
} else {
list -> right = last; //删除了最后一个结点
}
if (pos == 1) {
list -> left = next;
}
free(node);
}
```