圆形XOR链表?

时间:2012-06-12 10:53:28

标签: algorithm data-structures linked-list xor-linkedlist

我正在阅读关于XOR链表的问题,我想到了一个问题Is it possible to have a circular XOR linked list?在我看来,即使我们以某种方式构建了这样一个列表,但鉴于列表的头节点,也不可能遍历它。例如 - 让链表包含3个节点:A,B和C.

|
v
A ---> B ---> C

A->xor = B ^ C
B->xor = A ^ C
C->xor = A ^ B

由于在这种情况下我们得到head列表A,我们将无法向前或向后移动,因为我们必须知道至少一个{{1} 1}}或B以便移动。由于我们无法遍历它,我们也无法构建它。

我的想法是正确的吗?或者我错过了什么?

3 个答案:

答案 0 :(得分:4)

确实你是对的,因为我们无法从xor链接中检索任何指针值,所以无法遍历此列表。

此列表变为可遍历的最低要求是两个信息...例如指向当前节点的指针,以及指向下一个(或上一个)节点的指针。 然后您可以从xor链接中检索所有信息。

事实上,这就是Wikipedia article所说的:

  

要从某个点开始遍历任一方向的列表,您需要两个连续项的地址,而不仅仅是一个

这仍然比为每个节点存储两个指针便宜,因为我们每个节点只需要一个链接,加上当前和下一个(或前一个)节点的两个指针。

答案 1 :(得分:0)

这很有趣!我写了一些循环XOR链表程序,以证明它是可能的。 1个节点和2个节点的边缘情况有些奇怪,但是只要您跟踪某个地方的头和尾指针,其他所有内容都会检出。 (我也知道这是一个已有7年历史的线程,但是我有同样的问题,发现这几乎是唯一的关于循环XOR链表的提及)

#include <stdlib.h> // for malloc
#include <stdio.h>  // for printf
#include <stdint.h> // for uintptr_t

typedef struct  s_list  t_list;

struct s_list
{
    char        *data;  // Contains a string.
    struct s_list   *npx;   // npx = previous XOR next
};

t_list  *create_elem(char *data, t_list *npx)
{
    t_list  *ret;

    ret = malloc(sizeof(*ret));
    ret->data = data;
    ret->npx = npx;
    return (ret);
}

t_list  *xor(t_list *a, t_list *b)
{
    return (t_list*)((uintptr_t)a ^ (uintptr_t)b);
}

void    insert(t_list **h, t_list **t, char *data)
{
    t_list  *last = *t;
    t_list  *first = *h;
    t_list  *new, npx;

    if (!last && !first)  // No nodes, populate first node
    {
        new = create_elem(data, NULL);  // self XOR self == NULL
        *h = ((*t = new));
    }
    else if (last == first)  // Only one node, set tail properly
        *t = create_elem(data, NULL);   // self XOR self == NULL
    else  // Multiple nodes, do a real add
    {
        // Create an element with npx = first XOR last
        // (it will be inserted at the end of the list)

        new = create_elem(data, xor(first, last));

        // If head or tail's npx == 0, we know its a list of size 2,
        // so each prev and next pointer is the same.
        // So, if it is a list with size 2
        //  last->npx = new XOR first
        //  first->npx = new XOR last
        // else
        //  last->npx = new XOR (last->npx XOR first)
        //  first->npx = new XOR (first->npx XOR last)

        last->npx = xor(new, ((!last->npx || !first->npx) ?
                first : xor(last->npx, first)));
        first->npx = xor(new, ((!last->npx || !first->npx) ?
                last : xor(first->npx, last)));

        // Set the new pointers for passed addresses.
        *h = first;
        *t = new;
    }
}

int traverse(t_list *h, t_list *t)
{
    t_list  *cur = h;
    t_list  *last = t;
    t_list  *tmp;

    while (cur)
    {
        printf("[%s]\n", cur->data);
        tmp = xor(cur->npx, last);
        last = cur;
        cur = tmp;
        if (cur == h)
            return (1);
    }
    return (1);
}

int main(void)
{
    char    s1[] = "Testing!";
    char    s2[] = "My!";
    char    s3[] = "Function!";
    char    s4[] = "For!";
    char    s5[] = "GeeksforGeeks!";

    // We need to keep track of head and tail pointers for
    // everything to work nicely.

    // Traversal will always require access to
    // two consecutive pointers.

    t_list  *head;
    t_list  *tail;

    head = NULL;
    tail = NULL;
    insert(&head, &tail, s1);
    insert(&head, &tail, s2);
    insert(&head, &tail, s3);
    insert(&head, &tail, s4);
    insert(&head, &tail, s5);
    traverse(head, tail);
}

答案 2 :(得分:0)

不可能用一个值遍历列表,但是可以找到所有值 遍历会起作用,在这种情况下,我们发现所有 都能遍历树的b和c:

a=5
for b in range(0, 16): 
    print(a, b, a ^ b)

5 0 5
5 1 4
5 2 7
5 3 6
5 4 1
5 5 0
5 6 3
5 7 2
5 8 13
5 9 12
5 10 15
5 11 14
5 12 9
5 13 8
5 14 11
5 15 10
相关问题