gcc编译 DestroyList函数报错如下:
hello.c: In function ‘ClearList’:
hello.c:22:6: error: invalid type argument of ‘->’ (have ‘struct no11d’)
(*L)->next = *L;
^
hello.c:23:6: error: invalid type argument of ‘->’ (have ‘struct no11d’)
(*L)->prev = *L;
代码如下:
#include <stdio.h>
#include <stdlib.h>
typedef struct no11d
{
int data;
struct no11d* prev;
struct no11d* next;
}node_t, *pnode_t;
void ClearList(pnode_t L)
{
pnode_t p = L->next;//p指向链表第一个元素
while(p != L)
{
p = p->next;
free(p->prev);
}
(*L)->next = *L;
(*L)->prev = *L;
}
void DestroyList(pnode_t *L)
{
ClearList(*L);
free(*L);
*L = NULL;
}
void createLinklist(pnode_t *L)
{
*L = (pnode_t)malloc(sizeof(node_t));
if(*L == NULL)
exit(0);
(*L)->data = -1;
(*L)->prev = *L;
(*L)->next = *L;
}
void create_feature_new_node(pnode_t *L, int data)
{
*L = (pnode_t)malloc(sizeof(node_t));
if (*L == NULL) {
printf("create upload feature link list node error\n");
exit(1);
}
(*L)->data = data;
(*L)->prev = *L;
(*L)->next = *L;
}
void InsertNodeHead(pnode_t phead, int data)
{
pnode_t pnew = NULL;
create_feature_new_node(&pnew, data);
pnew->prev = phead;
pnew->next = phead->next;
phead->next->prev = pnew;
phead->next = pnew;
}
void printLinklist(pnode_t L)
{
printf("3--------------\n");
while (L->next->data != -1) {
L = L->next;
printf("%d\n", L->data);
}
printf("\n");
}
int main()
{
pnode_t phead = NULL;
createLinklist(&phead);
printf("1--------------\n");
InsertNodeHead(phead, 1);
printf("2--------------\n");
InsertNodeHead(phead, 2);
InsertNodeHead(phead, 3);
InsertNodeHead(phead, 4);
printLinklist(phead);
DestroyList(&phead);
}