关于头插法的图片理解可以看我的另一篇文章
代码如下:(一定要用图理解)更为清晰
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
//头插法
typedef struct _Node
{
int data;
struct _Node* next;
}Node;
Node* Createlist(int n)
{
int i = 0;
Node* head = NULL, * p = NULL;
for (i = 0; i < n; i++)
{
p = (Node*)malloc(sizeof(Node));
p->next = NULL;
scanf("%d", &p->data);
p->next = head;
head = p;
}
return head;
}
void Printlist(Node* head)
{
Node* p = head;
while (p)
{
printf("%d -> ", p->data);
p = p->next;
}
printf("NULL\n");
}
int main()
{
int n;
scanf("%d", &n);
Node* head;
head = Createlist(n);
Printlist(head);
return 0;
}