牛叔叔 的笔记

好好学习

2022-03-21 23:30

C语言头插法链表

牛叔叔

其它

(463)

(0)

收藏

上代码

#include <stdio.h>
#include <malloc.h>
typedef struct _Node{
	int num;
	_Node *next;
}Node;
Node* initNode(Node *node){
	node = (Node*)malloc(sizeof(Node));
	node->next = NULL;
	return node;
}
void addNode(Node *headnode,int a[],int num){
	Node *s;
	for(int i=0;i<num;i++){
		s = (Node*)malloc(sizeof(Node));
		s->num = a[i];
		s->next = headnode->next;
		headnode->next = s;
		
	}

}
void displayList(Node *node){
	Node *p = node->next;
	while(p!=NULL){
		printf("%d\n",p->num);
		p = p->next;
	}
}
void main(){
	Node *headnode = NULL;
	headnode = initNode(headnode);
	int a[] = {10,20,30,40,50,60};
	addNode(headnode,a,6);
	displayList(headnode);
}


0条评论

点击登录参与评论