如何将 link 列表传递给 c 中的函数
How to pass link list to a function in c
如何将 link 列表的头指针传递给函数?我写了 2 个程序在最后的 link 列表中插入 10 个元素。其中一个运行成功,另一个运行失败。我可以用我的第二个代码找出问题,但我找不到解决方案这是我的代码及其输出。
代码1(成功)-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int item;
struct node *next;
}snode;
void main()
{
system("clear");
snode *head,*p,*new,*last;
int i;
last=(snode *)malloc(sizeof(snode));
head=(snode *)malloc(sizeof(snode));
head->next=NULL;
last->next=NULL;
printf("Enter 10 numbers to be inserted at the end\n");
for(i=0;i<=9;i++)
{
new=(snode *)malloc(sizeof(snode));
scanf("%d",&new->item);
if(i==0)
{
head=last=new;
}
else
{
last->next=new;
new->next=NULL;
last=new;
}
}
p=head;
printf("Items in the link list are: ");
while(p!=NULL)
{
printf("%d->",p->item);
p=p->next;
}
printf("NULL\n");
}
输出-
Enter 10 numbers to be inserted at the end
0 1 2 3 4 5 6 7 8 9
Items in the link list are: 0->1->2->3->4->5->6->7->8->9->NULL
代码 2(失败)- insert 函数所做的更改未反映在 main() 中
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int item;
struct node *next;
}snode;
void insert(snode *,snode *);
void main()
{
system("clear");
snode *head,*p,*last;
int i;
last=(snode *)malloc(sizeof(snode));
head=(snode *)malloc(sizeof(snode));
(head)->next=NULL;
(last)->next=NULL;
insert(head,last);
p=head;
printf("Items in the link list are: ");
while(p!=NULL)
{
printf("%d->",p->item);
p=p->next;
}
printf("NULL\n");
}
void insert(snode *head,snode *last)
{
int i;
snode *new;
printf("Enter 10 numbers to be inserted at the end\n");
for(i=0;i<=9;i++)
{
new=(snode *)malloc(sizeof(snode));
scanf("%d",&new->item);
if(i==0)
{
head=last=new;
}
else
{
(last)->next=new;
new->next=NULL;
last=new;
}
}
}
输出-
Enter 10 numbers to be inserted at the end
0 1 2 3 4 5 6 7 8 9
Items in the link list are: 0->NULL
我知道我应该使用引用调用方法。但是我无法理解我在哪里使用 *
运算符和 &
运算符。
你的函数insert
通过值获取指针,所以当它修改head时,它修改了指针的本地副本。 insert
不会更改您在 main
中定义的 head
变量。
您需要将insert
更改为通过引用获取指针:
void insert(snode **head, snode **last);
然后在main中传递指针的地址:
insert(&head, &last);
查看您的代码,我发现您将 head 和 last 初始化为 malloc 结构。你确定你想要那个吗?通常你为空列表设置 head=last=NULL。
顺便说一句,你应该用高警告级别编译。这有助于您识别错误。
如何将 link 列表的头指针传递给函数?我写了 2 个程序在最后的 link 列表中插入 10 个元素。其中一个运行成功,另一个运行失败。我可以用我的第二个代码找出问题,但我找不到解决方案这是我的代码及其输出。
代码1(成功)-
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int item;
struct node *next;
}snode;
void main()
{
system("clear");
snode *head,*p,*new,*last;
int i;
last=(snode *)malloc(sizeof(snode));
head=(snode *)malloc(sizeof(snode));
head->next=NULL;
last->next=NULL;
printf("Enter 10 numbers to be inserted at the end\n");
for(i=0;i<=9;i++)
{
new=(snode *)malloc(sizeof(snode));
scanf("%d",&new->item);
if(i==0)
{
head=last=new;
}
else
{
last->next=new;
new->next=NULL;
last=new;
}
}
p=head;
printf("Items in the link list are: ");
while(p!=NULL)
{
printf("%d->",p->item);
p=p->next;
}
printf("NULL\n");
}
输出-
Enter 10 numbers to be inserted at the end
0 1 2 3 4 5 6 7 8 9
Items in the link list are: 0->1->2->3->4->5->6->7->8->9->NULL
代码 2(失败)- insert 函数所做的更改未反映在 main() 中
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int item;
struct node *next;
}snode;
void insert(snode *,snode *);
void main()
{
system("clear");
snode *head,*p,*last;
int i;
last=(snode *)malloc(sizeof(snode));
head=(snode *)malloc(sizeof(snode));
(head)->next=NULL;
(last)->next=NULL;
insert(head,last);
p=head;
printf("Items in the link list are: ");
while(p!=NULL)
{
printf("%d->",p->item);
p=p->next;
}
printf("NULL\n");
}
void insert(snode *head,snode *last)
{
int i;
snode *new;
printf("Enter 10 numbers to be inserted at the end\n");
for(i=0;i<=9;i++)
{
new=(snode *)malloc(sizeof(snode));
scanf("%d",&new->item);
if(i==0)
{
head=last=new;
}
else
{
(last)->next=new;
new->next=NULL;
last=new;
}
}
}
输出-
Enter 10 numbers to be inserted at the end
0 1 2 3 4 5 6 7 8 9
Items in the link list are: 0->NULL
我知道我应该使用引用调用方法。但是我无法理解我在哪里使用 *
运算符和 &
运算符。
你的函数insert
通过值获取指针,所以当它修改head时,它修改了指针的本地副本。 insert
不会更改您在 main
中定义的 head
变量。
您需要将insert
更改为通过引用获取指针:
void insert(snode **head, snode **last);
然后在main中传递指针的地址:
insert(&head, &last);
查看您的代码,我发现您将 head 和 last 初始化为 malloc 结构。你确定你想要那个吗?通常你为空列表设置 head=last=NULL。
顺便说一句,你应该用高警告级别编译。这有助于您识别错误。