单链表的创建(无表头 头插法)

单链表的创建(无表头 头插法),第1张

#define LEN sizeof(node)
typedef struct Node{
	int data;
	struct Node * next;
}node;//定义结点node
node * creat_link(int n){
	node * head = (node *)malloc(LEN);
	node * h = (node *)malloc(LEN);
	h = head;
	head->next = NULL;
	for (int i = 1; i <=n; i++){
		node * p = (node *)malloc(LEN);
		h->data = i;
		p->next = h;
		h= p;
	}
	return h->next;
}

void printf_link(node *head,int n){
	node * p = (node *)malloc(LEN);
	p = head;
	while(p){
		printf("%d\n", p->data);
		p = p->next;
	}
}
int main(){

	node * head = (node*)malloc(LEN);
	 head = creat_link(10);
	printf_link(head,10);
    return 0;
}

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/584423.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-12
下一篇2022-04-12

发表评论

登录后才能评论

评论列表(0条)

    保存