2012-04-03 103 views
0

是我的XOR鏈表實現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; 
} 

//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; 
} 

但代碼時,我編譯它,它給了我這樣的

1>------ Build started: Project: xor_list, Configuration: Debug Win32 ------ 
1> xor_list.cpp 
1>c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(30): error C2872: 'end' : ambiguous symbol 
1>   could be 'c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(9) : node *end' 
1>   or  'end' 
1>c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(69): error C2872: 'end' : ambiguous symbol 
1>   could be 'c:\users\daviti\documents\visual studio 2010\projects\xor_list\xor_list\xor_list.cpp(9) : node *end' 
1>   or  'end' 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

錯誤爲什麼它是ambigious符號到底是什麼?這個錯誤是什麼意思?

+0

將'end'節點變量名重命名爲其他內容,如'finish',並重新編譯 – Jaywalker 2012-04-03 07:59:00

回答

5

編譯器不確定是使用您的end全局變量還是std::end符號,它恰好是將迭代器返回到容器末尾的函數。將任何東西分配給該符號沒有多大意義,這是另一回事。刪除using namespace std;指令來解決這個特定的問題。我建議用using std::cout;替換它以避免污染命名空間。

1

在您的項目中似乎還有另一個符號end。你必須重命名符號。

或者你可能有一個擴展或插件,將end定義爲關鍵字。

1

要麼你必須改變你的變量名才能消除歧義,當您使用

using namespace std;

或 你必須聲明你到底是std::end不包括using namespace std;

1

這裏是我使用單個異或指針實現雙向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; 
}