```c++
#include "link.h"
#include <iostream>
# include <String>
# include <Cstring>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct PERSON{
char name[64];
int age;
int score;//typedef
}Person;
void myprint(void* data) {
// PERSON p;
//p= data;
/*实际上是在定义一个函数,针对person类型的入参设计打印*/
Person* p = (Person*)data;
printf("name;%s age;%d score;%d\n",p->name,p->age,p->score);
};
//.h文件定义一类指针,此处定义一个指针指向函数myprint行不行?
//void(*p)(Person data);
//初始化
linklist* init_linklist() {
linklist* list = (linklist*)malloc(sizeof(linklist));
list->size = 0;
list->head = (linklistnode*)malloc(sizeof(linklistnode));
list->head->data = NULL;//头节点不保存数据
list->head->next = NULL;
return list;
};
//插入
void insert_linklist(linklist* list, int pos, void* data) {
if (list == NULL) {
return;
}
if (data == NULL) {
return;
}
if (pos<0 || pos>list->size) {
// return;
//友好处理默认插入到尾部
pos=list->size;
}
linklistnode* newnode = (linklistnode*)malloc(sizeof(linklistnode));
newnode->data = data;
newnode->next = NULL;
//找节点 辅助指针变量
//linklistnode* pCurrent = list->head->next;
linklistnode* pCurrent = list->head;
//找到要插入得节点前一个节点
for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next;
}
newnode->next = pCurrent->next;
pCurrent->next = newnode;
list->size++;
};
//释放
void freespace_linklist(linklist* list) {
if (list == NULL) {
return;
}
linklistnode* pCurrent = list->head;
while (pCurrent != NULL) {
//缓存下一个节点
linklistnode* pNext = pCurrent->next;
free(pCurrent);
pCurrent = pNext;
}
list->size = 0;
free(list);
};
//打印
void printf_linklist(linklist* list, printfnode print) {
if (list == NULL) {
return;
}
//else
//找节点 辅助指针变量
linklistnode* pCurrent =list->head;
//list->head不为空,但是list->head->data与list->head->next都为NULL
while (pCurrent != NULL) {
print(pCurrent->data);//调用入参指针指向的函数
pCurrent = pCurrent->next;
}
};
int main()
{
linklist* list = init_linklist();
Person p1 = { "aaa",18,100 };
Person p2 = { "bbb",19,99 };
Person p3 = { "ccc",17,95 };
Person p4 = { "ddd",18,96 };
Person p5 = { "eee",20,89 };
Person p6 = { "fff",22,56 };
insert_linklist(list, 0, &p1);
insert_linklist(list, 0, &p2);
insert_linklist(list, 0, &p3);
insert_linklist(list, 0, &p4);
insert_linklist(list, 0, &p5);
insert_linklist(list, 0, &p6);
printf_linklist(list, myprint);
freespace_linklist(list);
system("pause");
}
```
运行后发生异常:读取访问权限冲突。 p 是 nullptr,但是已经把myprint中的data进行了强转,p应该指向person类型的结构体啊
#include "link.h"
#include <iostream>
# include <String>
# include <Cstring>
#include <stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct PERSON{
char name[64];
int age;
int score;//typedef
}Person;
void myprint(void* data) {
// PERSON p;
//p= data;
/*实际上是在定义一个函数,针对person类型的入参设计打印*/
Person* p = (Person*)data;
printf("name;%s age;%d score;%d\n",p->name,p->age,p->score);
};
//.h文件定义一类指针,此处定义一个指针指向函数myprint行不行?
//void(*p)(Person data);
//初始化
linklist* init_linklist() {
linklist* list = (linklist*)malloc(sizeof(linklist));
list->size = 0;
list->head = (linklistnode*)malloc(sizeof(linklistnode));
list->head->data = NULL;//头节点不保存数据
list->head->next = NULL;
return list;
};
//插入
void insert_linklist(linklist* list, int pos, void* data) {
if (list == NULL) {
return;
}
if (data == NULL) {
return;
}
if (pos<0 || pos>list->size) {
// return;
//友好处理默认插入到尾部
pos=list->size;
}
linklistnode* newnode = (linklistnode*)malloc(sizeof(linklistnode));
newnode->data = data;
newnode->next = NULL;
//找节点 辅助指针变量
//linklistnode* pCurrent = list->head->next;
linklistnode* pCurrent = list->head;
//找到要插入得节点前一个节点
for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next;
}
newnode->next = pCurrent->next;
pCurrent->next = newnode;
list->size++;
};
//释放
void freespace_linklist(linklist* list) {
if (list == NULL) {
return;
}
linklistnode* pCurrent = list->head;
while (pCurrent != NULL) {
//缓存下一个节点
linklistnode* pNext = pCurrent->next;
free(pCurrent);
pCurrent = pNext;
}
list->size = 0;
free(list);
};
//打印
void printf_linklist(linklist* list, printfnode print) {
if (list == NULL) {
return;
}
//else
//找节点 辅助指针变量
linklistnode* pCurrent =list->head;
//list->head不为空,但是list->head->data与list->head->next都为NULL
while (pCurrent != NULL) {
print(pCurrent->data);//调用入参指针指向的函数
pCurrent = pCurrent->next;
}
};
int main()
{
linklist* list = init_linklist();
Person p1 = { "aaa",18,100 };
Person p2 = { "bbb",19,99 };
Person p3 = { "ccc",17,95 };
Person p4 = { "ddd",18,96 };
Person p5 = { "eee",20,89 };
Person p6 = { "fff",22,56 };
insert_linklist(list, 0, &p1);
insert_linklist(list, 0, &p2);
insert_linklist(list, 0, &p3);
insert_linklist(list, 0, &p4);
insert_linklist(list, 0, &p5);
insert_linklist(list, 0, &p6);
printf_linklist(list, myprint);
freespace_linklist(list);
system("pause");
}
```
运行后发生异常:读取访问权限冲突。 p 是 nullptr,但是已经把myprint中的data进行了强转,p应该指向person类型的结构体啊