2015-11-21 37 views
-1

我有一個在unix平臺上創建的雙鏈表的程序,它只能找到。我只是複製粘貼代碼到我的Mac上的日食。對於一些奇怪的原因代碼運行正常,但無論何時添加或刪除或根本什麼做它總是顯示列表中的所有指標爲0我的雙鏈表中的所有內容都變成了0

int main() 
{ 
    list l = create_list(); 
    prepend (&l, (void*)1); 
    prepend (&l, (void*)2); 
    prepend (&l, (void*)55); 
return 0; 
} 

void display_list(list l) 
{ 
int i; 
for(i=0;i<size(l);i++) 
{ 
printf("Index [%d]: ",i); 
printf("%d",get(l,i)); 
printf("\n"); 
} 

} 

它會打印出

Index [0]: 0 
Index [1]: 0 
Index [2]: 0 

它的工作原理罰款UNIX,所以我不認爲它的方法,但我不知道什麼與它

的前置方法回事:

int prepend (list* l, void* item) 
{ 
int result = 0; 
if (l !=NULL) 
{ 
node* temp = malloc(sizeof(node)); 
if (temp != NULL) 
{ 
result = 1; 
temp -> item = item; 
if (l-> front == NULL) 
{ 
temp -> next = NULL; 
temp -> prev = NULL; 
l -> front = temp; 
l -> rear = temp; 
} 
else 
{ 
    temp -> next = l -> front; 
    temp -> prev = l -> rear; 
    l -> front= temp; 
} 
l -> size++; 
} 
} 
return result; 
} 

get方法:

void* get (list l, int location) 
{ 
void* item =NULL; 
if(1<=location && location<+ size(l)) 
{ 
node* temp = l.front; 
int i; 
for(i=1;i<location; i++) 
temp = temp -> next; 
item= temp -> item; 
} 
} 
+0

你傳遞一個指向常整型:(無效*)1,可能不處理跨編譯器的方式相同。 – nicomp

+1

你可以[編輯]你的問題,並添加'node'和函數'get'的定義? – usr2564301

+0

獲得(l,i)的回報是什麼?我的猜測是,基於體系結構,get()返回的數據類型與%d期望的內容不兼容。例如,如果%d期望一個32位整數並且返回一個64位整數。 – DBug

回答

0

首先,你似乎有點不一致。如果列表中已經有東西,那麼你將temp-> prev設置爲l->後面,這會形成一個循環雙向鏈表。但是,如果列表爲空,則添加新元素並將其next/prev設置爲NULL,從而使列表成爲非循環雙向鏈表。我會假設你想製作一個通告清單。

問題是,您不更新舊的l->前面的prev字段和l->後面的下一個字段。這應該是你在前面加上功能:

int prepend (list* l, void* item) 
{ 
    int result = 0; 
    if (l !=NULL) 
    { 
    node* temp = malloc(sizeof(node)); 
    if (temp != NULL) 
    { 
     result = 1; 
     temp -> item = item; 
     if (l-> front == NULL) 
     { 
     temp -> next = temp; 
     temp -> prev = temp; 
     l -> front = temp; 
     l -> rear = temp; 
     } 
     else 
     { 
     l -> front -> prev = temp; 
     l -> rear -> next = temp; 
     temp -> next = l -> front; 
     temp -> prev = l -> rear; 
     l -> front= temp; 
     } 
     l -> size++; 
    } 
    } 
    return result; 
} 

如果你想使一個非循環列表那麼這將是你的代碼:

int prepend (list* l, void* item) 
{ 
    int result = 0; 
    if (l !=NULL) 
    { 
    node* temp = malloc(sizeof(node)); 
    if (temp != NULL) 
    { 
     result = 1; 
     temp -> item = item; 
     if (l-> front == NULL) 
     { 
     temp -> next = NULL; 
     temp -> prev = NULL; 
     l -> front = temp; 
     l -> rear = temp; 
     } 
     else 
     { 
     l -> front -> prev = temp; 
     temp -> next = l -> front; 
     temp -> prev = NULL; 
     l -> front= temp; 
     } 
     l -> size++; 
    } 
    } 
    return result; 
} 

此外,鑑於你的問題的意見也適用:我假設get(l,i)將返回item字段,這是一個指針。如果您的平臺是64位的,但您嘗試將指針打印爲int(可能是32位),那麼您將遇到問題...只會打印指針的一半。

+0

我認爲我的64位平臺是問題,我該如何解決這個問題? – user135094

+0

和我用我的方法修正,它仍然沒有修復它 – user135094

+0

而不是使用'printf(「%d」,get(l,i));'嘗試使用'printf(「%ld」,get(l ,I));'。 – LaszloLadanyi

0

2個問題:

get()方法被定義爲返回一個void *,但沒有return語句,所以實際的返回值是任何數據在函數返回的位置堆棧。

您正在使用%d打印一個void *。 %d假設一個32位整數。 void *的是對x86-64的86 and64位32位,所以當你嘗試打印上的x86-64,%d只看上半部分,最顯著32位,我猜可能是全零。

所以要解決這個問題,解決得返回一個int(實際上是返回的東西)的返回類型,或改變的printf使用%P,這表明要打印的數據是一個指針。

相關問題