2.9单链表在pos之前插入
思路:
void SLInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x) { assert(pphead);//&plist assert(pos); //assert(*pphead); //一个节点 if (*pphead == NULL) { SLPushFront(pphead, x); } else//多个节点 { SLTNode* prev = *pphead; while (prev->next != pos) { prev = prev->next; } SLTNode* newnode = BuyLTNode(x); prev->next = newnode; newnode->next = pos; } }
执行:
2.10单链表在pos之后插入
思路:
void SLInsertAfter(SLTNode* pos, SLTDataType x) { assert(pos); SLTNode* newnode = BuyLTNode(x); newnode->next = pos->next; pos->next = newnode; }
2.11单链表删除pos的值
思路:
void SLErase(SLTNode** pphead, SLTNode* pos) { assert(pphead); assert(pos); if (pos == *pphead) { SLPopFront(pphead); } else { SLTNode* prev = *pphead; while (prev->next!=pos) { prev = prev->next; } prev->next = pos->next; free(pos); } }
2.12单链表删除pos之后的值
思路:
void SLEraseAfter(SLTNode* pos) { assert(pos); SLTNode* next= pos->next; pos->next = next->next; free(next); }
三.案例函数实现
3.1测试
void TestSList1() { SLTNode* plist = NULL; SLPushFront(&plist,1); SLPushFront(&plist,2); SLPushFront(&plist,3); SLPushFront(&plist,4); SLTPrint(plist); SLPushBack(plist, 5); SLTPrint(plist); }
3.2头插
void TestSList2() { SLTNode* plist = NULL; SLPushFront(&plist, 1); SLPushFront(&plist, 2); SLPushFront(&plist, 3); SLPushFront(&plist, 4); SLTPrint(plist); SLPushBack(&plist, 5); SLTPrint(plist); }
3.3尾插
void TestSList3() { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLTPrint(plist); SLPushBack(&plist, 2); SLTPrint(plist); SLPushBack(&plist, 3); SLTPrint(plist); SLPushBack(&plist, 4); SLTPrint(plist); }
3.4头删
void TestSList4() { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLTPrint(plist); //头删 SLPopFront(&plist); SLTPrint(plist); SLPopFront(&plist); SLTPrint(plist); SLPopFront(&plist); SLTPrint(plist); SLPopFront(&plist); SLTPrint(plist); }
3.5尾删
void TestSList5() { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLTPrint(plist); SLPopBack(&plist); SLTPrint(plist); SLPopBack(&plist); SLTPrint(plist); SLPopBack(&plist); SLTPrint(plist); }
3.6查找/修改某值
void TestSList6() { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLTPrint(plist); SLTNode* pos = STFind(plist,3); if (pos) pos->data = 30; SLTPrint(plist); }
3.7在pos之前插入
void TestSList7()//pos之前插入 { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLTPrint(plist); SLTNode* pos = STFind(plist,3); if (pos) { SLInsert(&plist, pos, 30); } SLTPrint(plist); }
3.8在pos之后插入
void TestSList8()//pos之后插入 { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLTPrint(plist); SLTNode* pos = STFind(plist, 3); if (pos) { SLInsertAfter(pos, 50); } SLTPrint(plist); }
3.9删除pos位置的值
void TestSList9() { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLTPrint(plist); SLTNode* pos = STFind(plist, 3); if (pos) { SLErase(&plist,pos); } SLTPrint(plist); }
3.10删除pos之后的值
void TestSList10() { SLTNode* plist = NULL; SLPushBack(&plist, 1); SLPushBack(&plist, 2); SLPushBack(&plist, 3); SLPushBack(&plist, 4); SLPushBack(&plist, 5); SLTPrint(plist); SLTNode* pos = STFind(plist, 2); if (pos) { SLEraseAfter(pos); } SLTPrint(plist); }
本章完,如有错误欢迎各位大佬指点,感谢你的来访。