为什么我的带有头节点的单链表输出不了内容?

用的是Dev-C++

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LENGTH 10
typedef struct ListNode{
    int value;
    ListNode *next;
}Node;
 
ListNode* CreateList(Node *head)//利用头插法创建一个单链表 
{
    ListNode *s,*p;
    head = (struct ListNode*)malloc(sizeof(struct ListNode));
    if (head == NULL) 
    {
        printf("申请空间失败!");
        exit(1);
    }
    head->next = NULL;
    s=head;
    srand(unsigned(time(NULL)));
    int k=1;
    while (k <= LENGTH)
       {
        p=(struct ListNode*)malloc(sizeof(struct ListNode));
        p->value=rand()%10;
        s = s->next;
        s = p;
        k+=1;
    }
    s->next=NULL;
}

void prt(ListNode *head)//显示数值 
{
    int a;
    ListNode *p,*q;
    p=head->next;
    printf("存储的数据如下:\n");
    for ( a = 0; a < LENGTH; a++)
    {
        printf("%d",p->value);
        q=p->next;
        free(p);
        p=q;
    }
    printf("\n");
}

void Insert(int i,int add,ListNode *head)//插入操作 
{
    ListNode *L;
    ListNode *v= (struct ListNode*)malloc(sizeof(struct ListNode));
    L=head;
    int j=1;
    i=i-1;
    if(i<0||i>LENGTH)
    {
        printf("输入了错误的位置!");
        exit(1);
    }
    while(L&&j<i)
    {
        L=L->next;
        j++;
    }
    v->next=L->next;
    L->next=v;
    L->next->value=add;
    free (L);
}

void Deleter(int i,ListNode *head)//删除操作 
{
    ListNode *L;
    int e;
    L=head;
    int j=0;
    if(i<0||i>LENGTH)
    {
        printf("输入了错误的位置!");
        exit(1);
    }
    while(L&&j<i)
    {
        L=L->next;
        j++;
    }
    L->next=L->next->next;
    e=L->next->value;
    free (L);
}

int main()
{
    ListNode *head;
    head->next=NULL; 
    CreateList(head);
    prt(head);
    int i;
    printf("请输入插入位置(不包括头结点):");
    scanf("%d",&i);
    int add;
    printf("请输入插入数据:");
    scanf("%d",&add);
    Insert(i,add,head);
    prt(head);
    printf("请输入删除位置(不包括头结点):");
    scanf("%d",&i);
    Deleter(i,head);
    prt(head);
    system("pause");
    return 0;
}

1. CreateList需要传入指针的地址。

2. 操作中不需要free()。

3. 有些地方逻辑不对,给你改好了。

具体为什么就不解释了,自己慢慢看慢慢理解吧!

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LENGTH 10
typedef struct ListNode {
	int value;
	ListNode *next;
}Node;

ListNode* CreateList(Node **head)   ////////////////二级指针
//利用头插法创建一个单链表 
{
	ListNode *s, *p;
	*head = (struct ListNode*)malloc(sizeof(struct ListNode));
	if (*head == NULL) {
		printf("申请空间失败!");
		exit(1);
	}
	(*head)->next = NULL;
	s = *head;
	srand(unsigned(time(NULL)));
	int k = 1;
	while (k <= LENGTH) {
		p = (struct ListNode*)malloc(sizeof(struct ListNode));
		p->value = rand() % 10;
		p->next = NULL;
		//s = s->next;///////////////////////////////////////////
		//s = p;
		s->next = p;
		s = p;
		k += 1;
	}
	s->next = NULL;
}

void prt(ListNode *head)//显示数值 
{
	int a;
	ListNode *p, *q;
	p = head->next;
	printf("存储的数据如下:\n");
	for (a = 0; a < LENGTH; a++) {
		printf("%d, ", p->value);
		q = p->next;
		//free(p);///////////////////////////////////
		p = q;
	}
	printf("\n");
}

void Insert(int i, int add, ListNode *head)//插入操作 
{
	ListNode *L;
	ListNode *v = (struct ListNode*)malloc(sizeof(struct ListNode));
	L = head;
	int j = 1;
	i = i - 1;
	if (i<0 || i>LENGTH) {
		printf("输入了错误的位置!");
		exit(1);
	}
	while (L&&j < i) {
		L = L->next;
		j++;
	}
	v->next = L->next;
	L->next = v;
	L->next->value = add;
	//free(L);
}

void Deleter(int i, ListNode *head)//删除操作 
{
	ListNode *L;
	int e;
	L = head;
	//int j = 0;	//////////////////////
	int j = 1;
	i = i - 1;
	if (i<0 || i>LENGTH) {
		printf("输入了错误的位置!");
		exit(1);
	}
	while (L&&j < i) {
		L = L->next;
		j++;
	}
	L->next = L->next->next;
	e = L->next->value;
	//free(L);/////////////////////////////////
}

int main() {
	ListNode *head = NULL;
	//head->next = NULL;//////////////////////////
	CreateList(&head);///////////////////////////
	prt(head);
	int i;
	printf("请输入插入位置(不包括头结点):");
	scanf_s("%d", &i);
	int add;
	printf("请输入插入数据:");
	scanf_s("%d", &add);
	Insert(i, add, head);
	prt(head);
	printf("请输入删除位置(不包括头结点):");
	scanf_s("%d", &i);
	Deleter(i, head);
	prt(head);
	system("pause");
	return 0;
}