使用二级指针申请链表

使用二级指针申请链表

#include <stdio.h>
typedef struct Node_t{
    
    
	int id;
	struct Node * next;
}Node;

void main(){
    
    
	Node * nodeList = NULL;
	Node * tmp = NULL;
	fun(&nodeList ,10);
	tmp = nodeList;
	while(tmp){
    
    
		printf("tmp:%d\n",tmp->id);
		tmp = tmp->next;
	}
	return ;
}
int fun(Node** list,int num){
    
    
	Node * node = NULL;
	int i = 0;
	for(i = 0; i < num; i++){
    
    
		node = malloc(sizeof(Node));
		if(node == NULL){
    
    
			return -1;
		}
		node->next = NULL;
		node->id = i;
		*list=node; // 这行相当于给外面的nodeList传了地址
		list = &(node->next);// 相当于node = node->next;
	}
}


猜你喜欢

转载自blog.csdn.net/G_Super_Mouse/article/details/109790264