单链表的增删查

#pragma once #include #include #include #includetypedef int DataType; typedef struct SListNode { DataType data; struct SListNode *pNext; }SListNode; //初始化 void SListNodeInit(SListNode **SL) { assert(SL); *SL= NULL; }//按值查找,返回第一个找到的结点指针,如果没找到,返回 NULL SListNode *SListNodeFind(SListNode *SL,DataType data) { SListNode *pNode = SL; while (pNode) { if (pNode->data =https://www.it610.com/article/= data) { return pNode; } pNode = pNode->pNext; } return NULL; }//尾插:申请空间(data=https://www.it610.com/article/data,pNext=NULL),找到倒数第一个,倒数第一个的pNEXT=NULL void SListNodePushBack(SListNode **SL, DataType data) { assert(SL); SListNode *pNewNode = (SListNode *)malloc(sizeof(SListNode)); assert(pNewNode); pNewNode->data = https://www.it610.com/article/data; pNewNode->pNext = NULL; if (*SL == NULL) { *SL = pNewNode; } SListNode *pNode = *SL; while (pNode->pNext) { pNode = pNode->pNext; } pNode->pNext = pNewNode; pNewNode->pNext = NULL; }//头插 void SListNodePushFront(SListNode **SL, DataType data) { assert(SL); SListNode *pNewNode = (SListNode *)malloc(sizeof(SListNode)); assert(pNewNode); pNewNode->data = https://www.it610.com/article/data; pNewNode->pNext = NULL; if (*SL == NULL) { SListNodePushBack(SL, data); } pNewNode->pNext = *SL; *SL = pNewNode; }//给定节点插入 void SListNodePushInsert(SListNode **SL,SListNode *Pos ,DataType data) { assert(*SL&&Pos); SListNode *pNode=*SL; if(Pos == *SL){ SListNodePushFront(SL,data); return; } else { while (pNode->pNext != Pos){ pNode = pNode->pNext; } SListNode *pNewNode = (SListNode *)malloc(sizeof(SListNode)); assert(pNewNode); pNewNode->data = https://www.it610.com/article/data; pNewNode->pNext = Pos; pNode->pNext = pNewNode; } }//尾部删除 void SListNodePopBack(SListNode **SL) { assert(*SL); assert(SL); if ((*SL)->pNext == NULL) { free(*SL); *SL = NULL; } SListNode *pNode = *SL; while (pNode->pNext->pNext !=NULL) { pNode = pNode->pNext; } pNode->pNext = NULL; free(pNode->pNext); }//头删: void SListNodePopFront(SListNode **SL) { assert(SL); assert(*SL); SListNode *pNode = *SL; *SL = (*SL)->pNext; free(pNode); }//给定节点删除 void SListNodeErase(SListNode **SL, SListNode *Pos) { assert(SL); assert(*SL); if (*SL == Pos) { SListNodePopFront(SL); return; } SListNode *pNode = *SL; while (pNode->pNext != Pos) { pNode = pNode->pNext; } pNode->pNext = Pos->pNext; free(Pos); }//按值删除遇到的第一个 void SListNodeRemove(SListNode **SL,DataType data) { assert(SL); assert(*SL); SListNode *pNode = SListNodeFind(*SL,data); if (pNode) { SListNodeErase(SL,pNode); } }//按值删除所有 void SListNodeRemoveAll(SListNode **SL, DataType data) { assert(SL); assert(*SL); SListNode *pNode=*SL; SListNode *next=NULL; while (pNode->pNext) { if (pNode->data =https://www.it610.com/article/= data){//替换法删除 next = pNode->pNext; pNode->pNext =next->pNext; pNode->data = https://www.it610.com/article/next->data; free(next); } pNode = pNode->pNext; } if ((*SL)->data =https://www.it610.com/article/= data) { SListNodePopFront(SL); } }//销毁 void SListNodeDestroy(SListNode **SL) { SListNode *pNode, *pNext; pNode = *SL; pNext = pNode->pNext; while (pNode != NULL){ pNode = pNode->pNext; free(pNode); pNode = pNext; } *SL= NULL; }//打印链表 void SListNodePrint(SListNode *SL) { assert(SL); SListNode *pNode; for (pNode = SL; pNode; pNode = pNode->pNext) { printf("%d->", pNode->data); } printf("NULL"); printf("\n"); }//测试 void test() { SListNode *SL; SListNodeInit(&SL); //尾插 SListNodePushBack(&SL, 3); SListNodePushBack(&SL, 4); SListNodePushBack(&SL, 5); SListNodePushBack(&SL, 6); SListNodePrint(SL); //头插 SListNodePushFront(&SL,2); SListNodePushFront(&SL,1); SListNodePrint(SL); //给定节点位置插入 SListNode *pNode = SListNodeFind(SL,3); SListNodePushInsert(&SL,pNode,5); SListNodePrint(SL); //尾删 SListNodePopBack(&SL); SListNodePrint(SL); //头删 SListNodePopFront(&SL); SListNodePrint(SL); //给定节点删除 SListNodeErase(&SL,pNode); SListNodePrint(SL); //给定值删除遇到的第一个 SListNodeRemove(&SL, 3); SListNodePrint(SL); //给定值删除所有 SListNodeRemoveAll(&SL,5); SListNodePrint(SL); }


    推荐阅读