【发布时间】:2015-10-12 02:54:04
【问题描述】:
我正在尝试制作一个内存高效的双向链表。该列表存储了下一个和上一个地址的 XOR,但我在函数 XOR 中遇到了错误。错误是:
[Error] cast from 'node*' to 'unsigned int' loses precision [-fpermissive]
我的代码是:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *next;
}*start,*temp;
node* XOR(node *a,node *b)
{
return (node *)((unsigned int)(a)^(unsigned int)(b));
}
void push(int data)
{
node *a=new node;
a->data=data;
a->next=XOR(start,NULL);
if(start!=NULL)
start->next=XOR(start->next,a);
start=a;
}
void disp()
{
temp=start;
node *prev,*cur;
while(temp)
{
cout<<temp->data<<" ";
if(temp==start)
{
prev=temp;
temp=temp->next;
}
else
{
cur=temp;
temp=XOR(temp->next,prev);
prev=cur;
}
}
}
main()
{
start=NULL;
push(1);
push(2);
push(3);
push(4);
push(5);
push(6);
push(7);
push(8);
}
【问题讨论】:
-
一些批评:
...你真的应该使用通常的标准头文件。在你的情况下,现在是 。 main() 也是 C。隐式 int 不是 C++ 功能,因此您的 main 至少应该是 int main()。现在您的错误:您正在编译 64 位,但 unsigned int 为 32。使其可移植包括并使用 std::uintptr_t.
标签: c++ xor doubly-linked-list