Link 列表错误,指针
Error in Link List, pointer
这段代码有什么问题?这只是创建一个 link 列表并打印它。
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *next;
int data;
};
void printLinklist(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d\t",temp->data);
temp = temp->next;
}
printf("\n");
}
void pushData(struct node *head, int val)
{
struct node *temp = malloc(sizeof (struct node));
temp->next = head;
temp->data = val;
head = temp;
}
int main()
{
struct node *head = NULL;
pushData(head, 1);
pushData(head, 2);
pushData(head, 3);
pushData(head, 4);
pushData(head, 5);
printLinklist(head);
}
我知道正确的做法。为此,我们必须在 pushData() 函数中传递 &head 并将该函数的参数定义为双指针(将 head 替换为 pushData() 函数中的 *head 并将 &head 作为参数传递)
我无法理解这段代码中的问题。
问题是您在 main()
中将 NULL
分配给了 head
,并且永远不会发生变化,它可以像这样工作
struct node *pushData(struct node *head, int val)
{
struct node *temp = malloc(sizeof (struct node));
if (temp == NULL)
return NULL;
temp->next = head;
temp->data = val;
return temp;
}
在主要部分
int main()
{
struct node *head;
head = pushData(head, 1);
head = pushData(head, 2);
head = pushData(head, 3);
head = pushData(head, 4);
head = pushData(head, 5);
printLinklist(head);
}
传递指针只修改指针指向的数据,而不修改指针本身。
所以在你的pushData()
函数中,你这样做
head = temp;
它改变了局部指针,并使它成为临时指针,但是一旦函数returns,改变就丢失了。
当你像 &head
一样传递 head
的地址时,然后你可以改变指针,因为你的 head
指针将指向指针本身而不是数据指针指向.
参数总是 passed by value
。当调用pushData()
时,head
的值,即指针变量中存储的地址被复制到函数的局部变量head
中。更改此项不会更改 main()
函数中的 head
变量。
如果您需要这样做,您必须传递 &head
,然后通过在 pushData()
函数中取消引用来分配值。
真实代码是:
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
struct node {
struct node* next;
int data;
};
void printLinklist(struct node* head)
{
struct node* temp = head;
while (temp->next != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
struct node* pushData(struct node* head, int val)
{
struct node* temp = malloc(sizeof(struct node));
if (temp == NULL)
return NULL;
temp->next = head;
temp->data = val;
return temp;
}
int main()
{
struct node* head = malloc(sizeof(struct node));
head = pushData(head, 1);
head = pushData(head, 2);
head = pushData(head, 3);
head = pushData(head, 4);
head = pushData(head, 5);
struct node* head2 = head;
for (int i = 0; i < 5; i++)
{
head2 = head2->next;
}
head2->next = NULL;
printLinklist(head);
}
这段代码有什么问题?这只是创建一个 link 列表并打印它。
#include<stdio.h>
#include<stdlib.h>
struct node{
struct node *next;
int data;
};
void printLinklist(struct node *head)
{
struct node *temp = head;
while(temp != NULL)
{
printf("%d\t",temp->data);
temp = temp->next;
}
printf("\n");
}
void pushData(struct node *head, int val)
{
struct node *temp = malloc(sizeof (struct node));
temp->next = head;
temp->data = val;
head = temp;
}
int main()
{
struct node *head = NULL;
pushData(head, 1);
pushData(head, 2);
pushData(head, 3);
pushData(head, 4);
pushData(head, 5);
printLinklist(head);
}
我知道正确的做法。为此,我们必须在 pushData() 函数中传递 &head 并将该函数的参数定义为双指针(将 head 替换为 pushData() 函数中的 *head 并将 &head 作为参数传递)
我无法理解这段代码中的问题。
问题是您在 main()
中将 NULL
分配给了 head
,并且永远不会发生变化,它可以像这样工作
struct node *pushData(struct node *head, int val)
{
struct node *temp = malloc(sizeof (struct node));
if (temp == NULL)
return NULL;
temp->next = head;
temp->data = val;
return temp;
}
在主要部分
int main()
{
struct node *head;
head = pushData(head, 1);
head = pushData(head, 2);
head = pushData(head, 3);
head = pushData(head, 4);
head = pushData(head, 5);
printLinklist(head);
}
传递指针只修改指针指向的数据,而不修改指针本身。
所以在你的pushData()
函数中,你这样做
head = temp;
它改变了局部指针,并使它成为临时指针,但是一旦函数returns,改变就丢失了。
当你像 &head
一样传递 head
的地址时,然后你可以改变指针,因为你的 head
指针将指向指针本身而不是数据指针指向.
参数总是 passed by value
。当调用pushData()
时,head
的值,即指针变量中存储的地址被复制到函数的局部变量head
中。更改此项不会更改 main()
函数中的 head
变量。
如果您需要这样做,您必须传递 &head
,然后通过在 pushData()
函数中取消引用来分配值。
真实代码是:
#include <stdio.h>
#include <stdlib.h>
#pragma warning(disable:4996)
struct node {
struct node* next;
int data;
};
void printLinklist(struct node* head)
{
struct node* temp = head;
while (temp->next != NULL)
{
printf("%d\t", temp->data);
temp = temp->next;
}
printf("\n");
}
struct node* pushData(struct node* head, int val)
{
struct node* temp = malloc(sizeof(struct node));
if (temp == NULL)
return NULL;
temp->next = head;
temp->data = val;
return temp;
}
int main()
{
struct node* head = malloc(sizeof(struct node));
head = pushData(head, 1);
head = pushData(head, 2);
head = pushData(head, 3);
head = pushData(head, 4);
head = pushData(head, 5);
struct node* head2 = head;
for (int i = 0; i < 5; i++)
{
head2 = head2->next;
}
head2->next = NULL;
printLinklist(head);
}