2013-04-16 30 views
-1

我嘗試訪問以前的某個不是結構或聯合的代碼: * head-> previous = newHead將我的鏈接列表轉換爲雙向鏈接列表,但在訪問上一個插入頭時遇到問題

我不太確定爲什麼...任何幫助非常感謝。

` 
struct ListNode 
{ 
    int value; 
    struct ListNode* next; 
    struct ListNode* previous; 
}; 

void insertAtHead(struct ListNode** head,int val) 
{ 
    struct ListNode* newHead = malloc(sizeof(struct ListNode)); 
    newHead->value = val; 
    newHead->next = NULL; 
    newHead->previous = NULL; 
    if(*head == NULL) 
     *head = newHead; 
    else 
    { 
     newHead->next = *head; 
     *head->previous = newHead; 
     *head = newHead; 
    } 
}` 
+1

嘗試'(* head) - > previous' – WhozCraig

回答

0

->操作員higher precedence*。所以, *head->previous被分組爲*(head->previous),這不是你想要的。使用括號:

(*head)->previous = newHead;