#include "myLinkList.h" /// 题目:在元素值等于key的结点前插入元素值为x的新结点 /// 1、定义遍历指针p,前驱指针pre指向p结点前驱,新结点指针pnew /// 2、遍历链表,结点值等于key时,在pre与p结点之间插入pnew结点 /// 3、值不等于key时,跳过该结点,继续遍历 void yyy_insert(myLNode* phead, linkType key, linkType x) { if (phead == NULL || phead->pnext == NULL) { return; } myLNode* p = phead->pnext; myLNode* pre = phead; myLNode* pnew = NULL; while (p != NULL) { if (p->data == key) { pnew = (myLNode*)malloc(sizeof(myLNode)); assert(pnew); pnew->data = x; pnew->pnext = pre->pnext; pre->pnext = pnew; } pre = p; p = p->pnext; } } void myprint(void* node) { if (node == NULL) { return; } linkType* p = (linkType*)node; printf("%d->", *p); } /// @brief 测试函数 void test() { /// 测试:init,input,output myLinkList mylist; xxx_init(&mylist); xxx_input(&mylist, 999); xxx_output(&mylist, myprint); printf("************************************************************\n"); yyy_insert(mylist.phead, 3, 10); xxx_output(&mylist, myprint); printf("************************************************************\n"); xxx_destroy(&mylist); } /// @brief main主函数 int main(int argc, char** argv) { test(); system("pause"); return 0; }
标签:pre,结点,mylist,myLNode,pnew,phead,测试 From: https://www.cnblogs.com/kxwslmsps/p/17121061.html