【发布时间】:2020-01-26 04:43:00
【问题描述】:
代码非常简单,也许我遗漏了一些明显导致分段错误的内容。乍一看,双向异或链表实现代码(打算)创建三个节点,并以从左到右的方式遍历它们。
#include <iostream>
using namespace std;
typedef struct node { int data = 0; struct node* npx = NULL; }n;
n *zor(n *a, n *b) {
return (n*)((uintptr_t) a ^ (uintptr_t) b);
}
int main() {
n *head, *a, *b;
head = new n;
a = new n;
b = new n;
head->npx = zor(NULL, a);
a->npx = zor(head, b);
b->npx = zor(a, NULL);
n* ptr = head;
while (ptr != NULL) {
cout << ptr->data;
ptr = zor(ptr, ptr->npx);
}
}
我希望在遍历列表中的所有节点后输出为“000”。
【问题讨论】:
-
您是否尝试过遍历您的代码?检查您存储在
ptr中的值是否是您认为的值?这似乎比花时间写一个问题更容易自己调试。 -
恐怕你试图让它有点太简单了,请在
ptr = zor(ptr, ptr->npx);付款,记住头节点的链接是用NULL, a设置的,而不是head, a。 Here's how I explained one of these antique brain-twisters last time.
标签: c++ linked-list doubly-linked-list xor-linkedlist