C语言:链表的三种传递信息方式

引言

以前看浙大翁恺老师的课时一直有一个关于链表的问题没有解决,现在想通了。
问题大致如下:

#include<stdio.h>
#include<stdlib.h>
tupedef struct _node
{
	int value;
	struct _node *next;
}Node;
void add(Node *head, int number);
int main(int argc, char const *argv[])
{
	Node * head = NULL;
	int number;
	do
	{
		scanf("%d", &number);
		if (number != -1)
		{
			add(head, number);
		}while (number != -1);
	}
	return 0;
}

void add(Node *head, int number)
{
	Node *p = (Node*)malloc(sizeof(Node));
	p->value = number;
	p->next = NULL;
	Node *last = head;
	if (last)
	{
		while (last->bext)
		{
			last = last->next;
		}
		last->next = p;
	}
	else
		head = p;
}

老师说这个函数是无效的,为什么呢?
因为当传进来的指针就是NULL的时候,做的事情是head = p,显然这时候就是无效的,但是其他情况都是有效的,因为其他情况是对这个Node*类型所指的变量做修改,并且函数里面的malloc出的空间存在stack里面,到另一个函数时是不会消失的,所以才可以这么操作,只要不是对传入指针head做操作,都是有效的。
下面我们用代码来验证一下:

#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
	int data;
	struct node *next;
}Node;
void _add_(struct node * phead);
void number(struct node * phead);
int main()
{
	struct node *head;
	head = (struct node*)malloc(sizeof(struct node));
	number(head);
	_add_(head);
	number(head);
	return 0;
}

void _add_(struct node * phead)
{
	struct node *current = (struct node*)malloc(sizeof(struct node));
	if (phead->next == NULL)
		phead->next = current;

	return;
}

void number(struct node * phead)
{
	struct node *current = phead;
	int i = 0;
	while (current != NULL)
	{
		i++;
		current = current->next;
	}
	printf("%d\n", i);

	return;
}

输出:
1
2
这就验证了我们的猜想,下面就拿引言中的代码来举例子。

传参的三种方式

一、全局变量

这种方式是最简便,因为我们不管在哪个函数里面都能对链表做修改,但是全局变量是有危害的,它会长期占用内存,污染命名空间,并且我们编写出来的函数只能针对这一个链表使用,当代码里面有多个链表的时候就没办法用这个函数去操作其他链表。不过它也有好处就是便捷,比较适合新手用。
具体操作:

#include<stdio.h>
#include<stdlib.h>
tupedef struct _node
{
	int value;
	struct _node *next;
}Node;
Node *head = NULL;
void add();
int main(int argc, char const *argv[])
{
	int number;
	do
	{
		scanf("%d", &number);
		if (number != -1)
		{
			add(head, number);
		}while (number != -1);
	}
	return 0;
}

void add()
{
	Node *p = (Node*)malloc(sizeof(Node));
	p->value = number;
	p->next = NULL;
	Node *last = head;
	if (last)
	{
		while (last->bext)
		{
			last = last->next;
		}
		last->next = p;
	}
	else
		head = p;
}

二、返回值

这个方法就是return一个head指针直接赋值给调用函数里的指针,它好用不过有时候可能会忘记在调用函数里面重新赋值。

#include<stdio.h>
#include<stdlib.h>
tupedef struct _node
{
	int value;
	struct _node *next;
}Node;
Node *add(Node *head, int number);
int main(int argc, char const *argv[])
{
	Node * head = NULL;
	int number;
	do
	{
		scanf("%d", &number);
		if (number != -1)
		{
			head = add(head, number);
		}while (number != -1);
	}
	return 0;
}

Node *add(Node *head, int number)
{
	Node *p = (Node*)malloc(sizeof(Node));
	p->value = number;
	p->next = NULL;
	Node *last = head;
	if (last)
	{
		while (last->bext)
		{
			last = last->next;
		}
		last->next = p;
	}
	else
		head = p;
	return p;
}

三、二级指针

最稳当的方法啦,直接对指针的地址进行操作,就像是刚学指针的时候在函数里面修改一个int类型的变量,就直接传它的指针,现在我们要操作的对象本来就是一个指针,那我们就传指针的指针,因为指针本身也就是一个变量。
具体操作:

#include<stdio.h>
#include<stdlib.h>
tupedef struct _node
{
	int value;
	struct _node *next;
}Node;
void add(Node **phead, int number);
int main(int argc, char const *argv[])
{
	Node * head = NULL;
	int number;
	do
	{
		scanf("%d", &number);
		if (number != -1)
		{
			add(&head, number);
		}while (number != -1);
	}
	return 0;
}

void add(Node **phead, int number)
{
	Node *p = (Node*)malloc(sizeof(Node));
	p->value = number;
	p->next = NULL;
	Node *last = *phead;
	if (last)
	{
		while (last->bext)
		{
			last = last->next;
		}
		last->next = p;
	}
	else
		*phead = p;
}
  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值