2.6 在指定位置之前插入数据
// 在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
分为两种情况:1. 插入的数据在链表中间;2. 插入的数据在链表的前面。
// 在指定位置之前插入数据
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{// 链表不能为空 *pphead != NULLassert(pphead && *pphead);assert(pos);// 申请新的节点SLTNode* newNode = SLTBuyNode(x);// 若pos == *pphead,说明是头插,调用头插函数接口if (pos == *pphead){SLTPushFront(pphead, x);}else{SLTNode* prev = *pphead;while (prev->next != pos){prev = prev->next;}newNode->next = pos;prev->next = newNode;}
}
测试程序:测试头节点之前插入
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试在指定位置之前插入数据// 先找下标,再插入//SLTNode* find = SLTFind(plist, 3);SLTNode* find = SLTFind(plist, 1); // 测试头节点之前插入//SLTNode* find = SLTFind(plist, 4);SLTInsert(&plist, find, 16);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
运行结果:
测试程序:测试链表中间插入
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试在指定位置之前插入数据// 先找下标,再插入SLTNode* find = SLTFind(plist, 3); // 测试头节点之前插入//SLTNode* find = SLTFind(plist, 1);//SLTNode* find = SLTFind(plist, 4);SLTInsert(&plist, find, 16);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
运行结果:
2.7 在指定位置之后插入数据
// 在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x);
// 在指定位置之后插入数据
void SLTInsertAfter(SLTNode* pos, SLTDataType x) // 不需要给头节点
{assert(pos);// 申请新的节点SLTNode* newNode = SLTBuyNode(x);newNode->next = pos->next;pos->next = newNode;
}
测试程序:
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试在指定位置之后插入数据SLTNode* find = SLTFind(plist, 1);SLTInsertAfter(find, 24);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
运行结果:
2.8 删除pos节点
// 删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos);
// 删除pos节点
void SLTErase(SLTNode** pphead, SLTNode* pos)
{// 链表不能为空 *pphead != NULLassert(pphead && *pphead);assert(pos);// pos是头节点if (pos == *pphead){这里就是头删接口函数//SLTNode* next = (*pphead)->next;//free(*pphead);//*pphead = next;SLTPopFront(pphead);}else{// pos不是头节点SLTNode* prev = *pphead;while (prev->next != pos){prev = prev->next;}prev->next = pos->next;free(pos);pos = NULL;}
}
测试程序:
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试删除pos节点//SLTNode* find = SLTFind(plist, 1);//SLTNode* find = SLTFind(plist, 4);SLTNode* find = SLTFind(plist, 3);SLTErase(&plist, find);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
运行结果:
2.9 删除pos之后的节点
// 删除pos之后的节点
void SLTEraseAfter(SLTNode* pos);
// 删除pos之后的节点
void SLTEraseAfter(SLTNode* pos)
{assert(pos && pos->next);// 先存要删除的节点地址SLTNode* del = pos->next;pos->next = pos->next->next;//pos->next = del->next;free(del);del = NULL;
}
测试程序:测试中间的节点
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试删除pos之后的节点//SLTNode* find = SLTFind(plist, 3);SLTNode* find = SLTFind(plist, 1); // 中间的节点SLTEraseAfter(find);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
运行结果:
测试程序:测试删除的是最后一个节点
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试删除pos之后的节点SLTNode* find = SLTFind(plist, 3); // 删除的是最后一个节点//SLTNode* find = SLTFind(plist, 1);SLTEraseAfter(find);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
运行结果:
2.10 销毁链表
// 销毁链表
void SListDestroy(SLTNode** pphead);
// 销毁链表
void SListDestroy(SLTNode** pphead)
{assert(pphead && *pphead);SLTNode* pcur = *pphead;while (pcur){SLTNode* next = pcur->next;free(pcur);pcur = next;}*pphead = NULL;
}
测试程序:
void SListTest02()
{SLTNode* plist = NULL;// 测试尾插SLTPushBack(&plist, 1);SLTPushBack(&plist, 2);SLTPushBack(&plist, 3);SLTPushBack(&plist, 4);SLTPrint(plist);// 测试删除pos之后的节点SLTNode* find = SLTFind(plist, 3); // 删除的是最后一个节点//SLTNode* find = SLTFind(plist, 1);SLTEraseAfter(find);SLTPrint(plist);// 销毁链表SListDestroy(&plist);SLTPrint(plist);
}int main()
{SListTest02();return 0;
}
调试结果:节点全部释放