2012-08-25 1396 views
4

我想排序一個鏈表。我對什麼時候使用struct node*head以及何時使用struct node **head感到困惑,可以使用它們兩者來完成實現。struct node * head和struct node ** head有什麼區別?

什麼時候應該使用:

void sortedinsert(struct node **head) 

和何時應使用:

void sortedinsert(struct node *head) 

回答

0

第一個是一個指針,指向這是一個結構節點。

(struct node *) head; 

head定義爲能夠存儲node的地址的變量。

這允許在方法中通過引用node

第二個是指向指向結構的節點的指針。

(struct node **) head; 

定義head作爲變量可存儲其具有node的地址另一個變量的地址。

這允許在方法中通過參考傳遞node *

0

node*是指向節點結構的指針。 node**是指向節點結構的指針。當您想要通過引用修改指針時,指針指針用於C中。

假設您想對節點B執行一項操作,該操作可能會將節點B替換爲其他節點。做事情的方法之一是

nodeA.next = foo(nodeA.next);

另一種選擇是隻是做

foo(&nodeA.next);

而且具有FOO隱含更換nodeA.next指針。

2

如果頭部始終在鏈接列表(恆定的位置)的頭指向然後用結構節點*頭 如果您打算改變由頭部使用節點**頭

+0

您也可以通過返回從可以改變它的任何功能的新的價值和分配結果改變頭。 (查看我的回答) – Antimony

1

struct node **head你所指的位置通過使指向/指向不同存儲區域的指針的地址通過head。隨着struct node *head您不能修改head到別的

6

指向任何地方使用該函數簽名:

void changeNode(struct node *head) 

你有一個指針的節點,這樣你就可以改變這種結構。你不能改變什麼變量頭指向。讓我們假設的struct node如下定義:

struct node 
{ 
    int field1; 
    struct node *next; 
} 

在給定的函數簽名和struct node,考慮下面的操作可以在功能改變結構:

void changeNode(struct node *head) 
{ 
    head->field1 = 7; 
    head->next = malloc(sizeof(struct node)); 
} 

C是通過按值:當我們將一個變量傳遞給一個函數時,函數得到一個拷貝。這就是爲什麼我們傳遞一個指向struct node的指針,以便我們可以改變它,並在函數外部產生這些更改的效果。但是我們仍然只獲得指針本身的副本。所以下面的操作是沒有用的:

void changeNode(struct node *head) 
{ 
    // we're only changing the copy here 
    head = malloc(sizeof(struct node)); 
} 

head的變化不會功能之外得到反映。爲了改變,我們必須用什麼head點額外的間接級別:

void changeNode(struct node **head) 
{ 
    // now we're changing head 
    *head = malloc(sizeof(struct node)); 

    // alternately, we could also do this: 
    *head = NULL; 
} 

現在更改head反映功能。

0

在本例中,由於頭的值可以改變(它可以指向從當前不同一些其他節點),而正在創建它,

您應該使用,

void sortedinsert(struct node **head) 
由於您的頭部可能需要修改,因此請使用

及以下protoptye需要改變,現在看來,

void sortedinsert(struct node *head) 

,因爲這不會允許你修改的頭,它應該是在這種情況下(如果你使用這個),

struct node * sortedinsert(struct node *head) 

它返回更新後的頭,它可以在此函數的調用者中使用。

0

運行或閱讀,你可以看到它

#include <stdio.h> 
struct node{ 
    int one; 
    int two; 
    struct node * next; 
    //char location[100]; 
}; 
void changeHead(struct node** head){ 

} 
void sort(struct node* head){ 

} 
int main(){ 
    struct node* head = (struct node*) (malloc (sizeof(struct node))); 

    // now head pointing to a node stucture (if you dereferance head you get teh first value) 

    struct node* tmp = head; 

    struct node** newHead = (struct node**) (malloc (sizeof(struct node*))); 

    //New head points to a 'struct node*', which hold an addtess of another struct node 

    head->one = 12;//*head->one =12; //head.one = 12 is wrong cos it is holding an address. 
    // you can do it but it doesnt make sence since you dont know whats on address #12 


    // now if you want head to point to old head which is on tmp at the moment 
    *newHead = head; 

    // now if you working with 2 linked list and you want to change the heads use below 
    changeHead(newHead); 

    // if you want to just sort its better and easy to use 
    sort(head); 

    //chack whats on head and newhead 
    printf("double derefence newHead:%d\n",**newHead); 
    printf("derefence newHead(its and address same ad address of head):%d\n",*newHead); 
    printf("Head(its and address):%d\n",head); 
    printf("derefence Head:%d\n",*head);//head->one works too 
} 
0

簡單地說用你**head當你想改變的「頭」指向的位置,否則使用*head它不允許改變位置。

0
  1. 要進行的更改是指向第 節點的頭,你需要通過頭部的地址,即(&head)調用 功能(所以你聲明指針的指針變量 在功能(struct node**head),因爲它接收地址 head這是什麼,但指針第一個節點。
  2. 然而,如果你知道,頭將保持固定我。e你不希望 頭指向某個其他節點(例如..沒有在 開始處插入一個新節點),那麼你只能通過(頭)調用函數 並在函數定義中編寫(*head)
相關問題