我正在尝试对链表进行排序。我对何时使用struct node *head
和何时使用struct node **head
感到困惑,这两种实现方法都可以实现。
应该在什么情况下使用:
void sortedinsert(struct node **head)
我应该在什么情况下使用:
void sortedinsert(struct node *head)
我正在尝试对链表进行排序。我对何时使用struct node *head
和何时使用struct node **head
感到困惑,这两种实现方法都可以实现。
应该在什么情况下使用:
void sortedinsert(struct node **head)
我应该在什么情况下使用:
void sortedinsert(struct node *head)
使用以下函数签名:
void changeNode(struct node *head)
你拥有指向该节点的指针,因此你可以更改该结构。但你无法改变变量head所指向的内容。假设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
进行的更改会在函数外部得到反映。
head
的地址,这使得它可以指向不同的内存区域。 使用struct node *head
无法将head
修改为指向其他地方。首先,这是一个指向结构体节点的指针。
(struct node *) head;
将head
定义为一个变量,它可以存储node
的地址。
这允许在方法中通过引用传递node
。
第二个是指向结构体node
的指针的指针。
(struct node **) head;
将head
定义为一个变量,它可以存储另一个变量的地址,该变量具有node
的地址。
这允许在方法中通过引用传递node *
。
void sortedinsert(struct node **head)
void sortedinsert(struct node *head)
head
指针的副本,因为您很可能会在内部使用head
变量。sortedinsert
函数很可能会使用head
参数调用。void sortedinsert(struct node **head)
由于您的头部可能需要修改。
而且下面的原型似乎需要更改,
void sortedinsert(struct node *head)
由于这不允许您修改头部,所以在这种情况下应该是这样的(如果您正在使用此功能),
struct node * sortedinsert(struct node *head)
该函数返回更新后的头部,可供调用该函数的程序使用。
运行或阅读此内容,您可以看到它
#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
}
(&head)
)传递给调用函数(因此在函数中声明了指向指针变量的指针(struct node**head
),因为它接收指向第一个节点的指针(head
)的地址。(*head)
)。node*
是指向节点结构体的指针。 node**
是指向指向节点结构体的指针的指针。在 C 中,当您想通过引用修改指针时,会使用指向指针的指针。
假设您想对节点 B 进行操作,该操作可能会用不同的节点替换节点 B。一种方法是
nodeA.next = foo(nodeA.next);
另一个选择是只需执行
foo(&nodeA.next);
并且让foo隐式地替换nodeA.next
指针。
**head
,否则在不允许更改位置的情况下,请使用 *head
。