在写c语言中双向链表时,每当运行到输入函数都会报错,显示head是空指针。不知道到底错在哪,请各位指点指点。
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
struct DoubleList {
int element;
struct DoubleList* next;
struct DoubleList* prior;
};
typedef struct DoubleList* List;
void initDoubleList(List head)
{
head->next = head->prior = NULL;
}
int insertDoubleList(List head, int element, int index) {
if (index < 1)
return 0;
while (--index)
{
head = head->next;
if (head == NULL)
return 0;
}
List node = malloc(sizeof(struct DoubleList));
if (node == NULL)
return 0;
node->element = element;
if (head->next!=NULL)
{
head->next->prior = node;
node->next = head->next;
}
else
{
node->next == NULL;
}
head->next = node;
node->prior = head;
return 1;
}
void print(List head)
{
while (head)
{
head = head->next;
printf("%d", head->element);
}
}
int main()
{
struct DoubleList head;
initDoubleList(&head);
for (int i = 1; i <= 3; i++)
insertDoubleList(&head, i * 100, 1);
print(&head);
List node = &head;
return 0;
}