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;
}
調試結果:節點全部釋放