ESP-IDF:传统链表测试
在ESP-IDF环境下简单地写个传统链表
/--------------Test 传统链表-------------/
//链表结点
typedef struct LINKNODE11 {
void * data;
LINKNODE11 * next;
}linknode11;
//链表头
typedef struct LINKLIST11 {
linknode11 * head;
int size;
}linklist11;
linklist11 * InitLinklist11() {
linklist11 * listp = (linklist11 *)malloc(sizeof(linklist11));
linknode11 * nodep = (linknode11 *)malloc(sizeof(linknode11));//创建一个空结点,方便写程序
listp->head = nodep;
listp->size = 0;
listp->head->data = NULL;
listp->head->next = NULL;
return listp;
}
void freeLinklist11(linklist11 *listp) {
if(listp == NULL) {
return;
}
linknode11 * pCurrent = listp->head; //辅助指针
// for (int i = 0; i <= listp->size; i++)
// {
// linknode11 *pNext = pCurrent->next; // 缓存下一个结点的地址
// free(pCurrent);
// pCurrent = pNext;
// }
while (pCurrent != NULL)
{
linknode11 *pNext = pCurrent->next; // 缓存下一个结点的地址
free(pCurrent);
pCurrent = pNext;
}
free(listp);
}
void insetLinklist11(linklist11 * listp,void * data,int pos) {
if(listp == NULL) {
return;
}
if(data == NULL) {
return;
}
linknode11 *nodep = (linknode11 *)malloc(sizeof(linknode11));
nodep->data = data;
nodep->next = NULL;
linknode11 * pCurrent = listp->head;
for(int i = 0; i< pos; i++) {
pCurrent = pCurrent->next;
}
nodep->next = pCurrent->next;
pCurrent->next = nodep;
listp->size++;
}
typedef struct PERSON11{
string name;
int age;
}person11;
typedef void (* PRINTLINK11) (void * data);
void printlink11(void * datap) {
person11 *p = (person11 *)datap;
cout << “名字是:” << p->name << " 年龄是:" << p->age << endl;
}
void printLinkList11(linklist11 * listp, PRINTLINK11 printlink11) {
if(listp == NULL) {
return;
}
linknode11 * pCurrent = listp->head->next;
for (int i = 0; i < listp->size; i++){
printlink11(pCurrent->data);
pCurrent = pCurrent->next;
}
}
void test11() {
person11 p1, p2, p3, p4, p5;
p1.name = “aaa”;
p2.name = “bbb”;
p3.name = “ccc”;
p4.name = “ddd”;
p5.name = “eee”;
p1.age = 10;
p2.age = 11;
p3.age = 12;
p4.age = 13;
p5.age = 14;
linklist11 * listp = InitLinklist11();
insetLinklist11(listp,&p1,0);
insetLinklist11(listp,&p2,0);
insetLinklist11(listp,&p3,0);
insetLinklist11(listp,&p4,0);
insetLinklist11(listp,&p5,0);
cout<<"--------------Test 传统链表-------------"<<endl;
printLinkList11(listp,printlink11);
freeLinklist11(listp);
}
extern “C” void app_main(void)
{
test11();
}