初始化函数
错误写法和分析
test.c
#include"List.h"
void test1()
{
LTNode* pList = NULL;
ListInit(pList);
}
int main()
{
test1();
return 0;
}
list.c
#include"List.h"
LTNode* BuyListNode(LTDataType x)
{
LTNode* node = (LTNode *)malloc(sizeof(LTNode));
if (node == NULL)
{
printf("malloc fail\n");
exit(-1);
}
node->data = x;
node->next = NULL;
node->prev = NULL;
}
void ListInit(LTNode* phead)
{
phead = BuyListNode(-1);
phead->next = phead;
phead->prev = phead;
return phead;
}
正确写法
test.c
#include"List.h"
void test1()
{
LTNode* pList = NULL;
ListInit(&pList);
}
int main()
{
test1();
return 0;
}
List.c
#include"List.h"
LTNode* BuyListNode(LTDataType x)
{
LTNode* node = (LTNode *)malloc(sizeof(LTNode));
if (node == NULL)
{
printf("malloc fail\n");
exit(-1);
}
node->data = x;
node->next = NULL;
node->prev = NULL;
}
void ListInit(LTNode** pphead)
{
*pphead = BuyListNode(-1);
(*pphead)->next = *pphead;
(*pphead)->prev = *pphead;
}
一级指针的写法
test.c
List.c
LTNode* ListInit()
{
LTNode* phead = BuyListNode(0);
phead->next = phead;
phead->prev = phead;
return phead;
}
判空函数
size函数
打印
//打印
void Listprint(LTNode* phead)
{
assert(phead);
LTNode *cur = phead->next;
while (cur != phead)
{
printf("%d", cur->data);
cur = cur->next;
}
printf("\n");
}
销毁
test.c
List.c
尾插
不需要改变plist和phead,改变的是phead指向的节点(head)的内容(prev、next)所以不需要二级指针
test.c
List.c
调试检查是否插入
尾删
判空函数控制链表为空还继续尾删报错
在pos之前插入(包含尾插头插)
test.c
list.c
删除pos位置
头插头删
销毁