C++ xor链表实现
下面是我的xor链表实现代码C++ xor链表实现,c++,xor-linkedlist,C++,Xor Linkedlist,下面是我的xor链表实现代码 #include<iostream> using namespace std; struct node { int v; node *next; }; node *start=NULL; node *end=NULL; node *newnode(int v) { node *np=new node; np->v=v; np->next=NULL; return np; } //ret
#include<iostream>
using namespace std;
struct node
{
int v;
node *next;
};
node *start=NULL;
node *end=NULL;
node *newnode(int v)
{
node *np=new node;
np->v=v;
np->next=NULL;
return np;
}
//returns the xor of two nodes
node *xor(node *a,node *b)
{
return (node*)((long long)(a)^(long long)(b));
}
void insert(node *current,node *prev,node *np)
{
if(start==NULL)
{
np->next=xor(prev,NULL);
start=np;
end=np;
}
else
{
insert(xor(prev,current->next),current,np);
}
}
void displayforward(node *start,node *prev)
{
if(start==NULL) return ;
cout<<start->v<<"-> ";
displayforward(xor(start->next,prev),start);
}
void displayBackward(node *end, node *prev){
if(end == NULL) return;
cout<< end->v<<" -> ";
displayBackward( xor(end->next, prev), end);
}
int main()
{
int a[] = {1,2,3,4,5,6,7,8,9,10}, n = 10;
for(int i=0; i < n; i++){
node *prev = NULL;
insert(start, prev, newnode(a[i]));
}
cout<<"Forward: \n";
node *prev=NULL;
displayforward(start, prev);
cout<<"\nBackward: \n";
displayBackward(end, prev);
return 0;
}
为什么它是含糊不清的符号end?这个错误是什么意思?在您的项目中似乎还有另一个名为end的符号。您必须重命名该符号
或者,您可能有一个将end定义为关键字的扩展或插件。编译器不确定是使用end全局变量还是std::end符号,后者恰好是一个将迭代器返回到容器末尾的函数。给那个符号分配任何东西都没有多大意义,这是另一回事。删除使用std的名称空间;指令来解决此特定问题。我建议用std::cout替换它;为了避免污染名称空间。当您使用 使用名称空间std 或
您必须将端声明为std::end,而不包括使用名称空间std 下面是我使用单异或指针实现的双LL。请提出你的意见。谢谢
#include <stdio.h>
#include <stdlib.h>
static struct node *nodeAlloc();
static struct node *xor(struct node *a, struct node *b);
static void insertFirst(int);
static void insertLast(int);
void dumpList(struct node *, struct node *);
struct node {
int value;
struct node *np;
};
struct node *head;
struct node *tail;
static struct node *nodeAlloc() {
return (struct node *)malloc(sizeof(struct node));
}
static struct node *xor(struct node *prev, struct node *next) {
return (struct node*)((unsigned long)prev ^ (unsigned long)next);
}
static void insertFirst(int value) {
struct node *x;
x = nodeAlloc();
x->value = value;
if(head == NULL && tail == NULL) {
x->np = NULL;
head = x;
tail = x;
} else {
x->np = xor(NULL, head);
head->np = xor(x, head->np);
head = x;
}
}
static void insertLast(int value) {
struct node *x;
x = nodeAlloc();
x->value = value;
if(head == NULL && tail == NULL) {
x->np = NULL;
head = x;
tail = x;
} else {
x->np = xor(tail, NULL);
tail->np = xor(tail->np, x);
tail = x;
}
}
void dumpList(struct node *node, struct node *prev) {
if(node != NULL) {
printf("\n%d", node->value);
dumpList(xor(node->np, prev), node);
}
}
//------------------
int main() {
insertFirst(7);
insertFirst(5);
insertFirst(4);
insertFirst(2);
insertLast(8);
insertLast(9);
insertLast(10);
printf("\n------------------\nForward List:");
dumpList(head, NULL);
printf("\n------------------\nBackward List:");
dumpList(tail, NULL);
return 0;
}
将结束节点变量名重命名为其他名称,如finish,然后重新编译
#include <stdio.h>
#include <stdlib.h>
static struct node *nodeAlloc();
static struct node *xor(struct node *a, struct node *b);
static void insertFirst(int);
static void insertLast(int);
void dumpList(struct node *, struct node *);
struct node {
int value;
struct node *np;
};
struct node *head;
struct node *tail;
static struct node *nodeAlloc() {
return (struct node *)malloc(sizeof(struct node));
}
static struct node *xor(struct node *prev, struct node *next) {
return (struct node*)((unsigned long)prev ^ (unsigned long)next);
}
static void insertFirst(int value) {
struct node *x;
x = nodeAlloc();
x->value = value;
if(head == NULL && tail == NULL) {
x->np = NULL;
head = x;
tail = x;
} else {
x->np = xor(NULL, head);
head->np = xor(x, head->np);
head = x;
}
}
static void insertLast(int value) {
struct node *x;
x = nodeAlloc();
x->value = value;
if(head == NULL && tail == NULL) {
x->np = NULL;
head = x;
tail = x;
} else {
x->np = xor(tail, NULL);
tail->np = xor(tail->np, x);
tail = x;
}
}
void dumpList(struct node *node, struct node *prev) {
if(node != NULL) {
printf("\n%d", node->value);
dumpList(xor(node->np, prev), node);
}
}
//------------------
int main() {
insertFirst(7);
insertFirst(5);
insertFirst(4);
insertFirst(2);
insertLast(8);
insertLast(9);
insertLast(10);
printf("\n------------------\nForward List:");
dumpList(head, NULL);
printf("\n------------------\nBackward List:");
dumpList(tail, NULL);
return 0;
}