当前位置:   article > 正文

单向链表的复杂操作

单向链表的复杂操作

1、删除链表节点

  1. int DeleteLinkList(LinkNode *pHead, DataType TmpData)
  2. {
  3. LinkNode *pPreNode = NULL;
  4. LinkNode *pTmpNode = NULL;
  5. int cnt = 0;
  6. pPreNode = pHead;
  7. pTmpNode = pHead->pNext;
  8. while (pTmpNode != NULL)
  9. {
  10. if (pTmpNode->Data == TmpData)
  11. {
  12. //删除
  13. pPreNode->pNext = pTmpNode->pNext;
  14. free(pTmpNode);
  15. pTmpNode = pPreNode->pNext;
  16. cnt++;
  17. }
  18. else
  19. {
  20. pTmpNode = pTmpNode->pNext;
  21. pPreNode = pPreNode->pNext;
  22. }
  23. }
  24. return cnt;
  25. }

2、销毁链表

  1. int DestroyLinkList(LinkNode **ppHead)
  2. {
  3. LinkNode *pTmpNode = NULL;
  4. LinkNode *pFreeNode = NULL;
  5. pTmpNode = pFreeNode = *ppHead;
  6. while (pTmpNode != NULL)
  7. {
  8. pTmpNode = pTmpNode->pNext;
  9. free(pFreeNode);
  10. pFreeNode = pTmpNode;
  11. }
  12. *ppHead = NULL;
  13. return 0;
  14. }

3、寻找链表的中间节点位置

  1. LinkNode *FindMidLinkNode(LinkNode *pHead)
  2. {
  3. LinkNode *pFast = NULL;
  4. LinkNode *pSlow = NULL;
  5. pFast = pSlow = pHead->pNext;
  6. while (pFast != NULL)
  7. {
  8. pFast = pFast->pNext;
  9. if (NULL == pFast)
  10. {
  11. break;
  12. }
  13. pFast = pFast->pNext;
  14. if (NULL == pFast)
  15. {
  16. break;
  17. }
  18. pSlow = pSlow->pNext;
  19. }
  20. return pSlow;
  21. }

功能:找到中间节点位置
算法原理:
快指针pFast每次走2步,慢指针pSlow每次走1步,快指针到达末尾时,慢指针所在的位置即为中间位置 。

4、寻找链表倒数第K个节点

  1. LinkNode *FindLastKthLinkNode(LinkNode *pHead, int k)
  2. {
  3. LinkNode *pFast = pHead->pNext;
  4. LinkNode *pSlow = pHead->pNext;
  5. int i = 0;
  6. for (i = 0; i < k; i++)
  7. {
  8. pFast = pFast->pNext;
  9. if (NULL == pFast)
  10. {
  11. break;
  12. }
  13. }
  14. if (NULL == pFast)
  15. {
  16. return NULL;
  17. }
  18. while (pFast != NULL)
  19. {
  20. pSlow = pSlow->pNext;
  21. pFast = pFast->pNext;
  22. }
  23. return pSlow;
  24. }

功能:找到链表倒数第k个节点
算法原理:
快指针先走k步,慢指针和快指针每次走1步(快指针总是领先慢指针k步),当快指针走到末尾时,慢指针即指向链表倒数第k个节点 。

5、倒置链表

  1. int ReversalLinkList(LinkNode *pHead)
  2. {
  3. LinkNode *pTmpNode = NULL;
  4. LinkNode *pInsertNode = NULL;
  5. pTmpNode = pHead->pNext;
  6. pHead->pNext = NULL;
  7. pInsertNode = pTmpNode;
  8. while (pTmpNode != NULL)
  9. {
  10. pTmpNode = pTmpNode->pNext;
  11. pInsertNode->pNext = pHead->pNext;
  12. pHead->pNext = pInsertNode;
  13. pInsertNode = pTmpNode;
  14. }
  15. return 0;
  16. }

6、链表的冒泡排序 

  1. int BubbleSortLinkList(LinkNode *pHead)
  2. {
  3. LinkNode *pTmpNode1 = NULL;
  4. LinkNode *pTmpNode2 = NULL;
  5. LinkNode *pEnd = NULL;
  6. DataType TmpData;
  7. //如果链表没有节点或者只有1个节点返回0
  8. if (NULL == pHead->pNext || NULL == pHead->pNext->pNext)
  9. {
  10. return 0;
  11. }
  12. while (1)
  13. {
  14. pTmpNode1 = pHead->pNext;
  15. pTmpNode2 = pHead->pNext->pNext;
  16. if (pTmpNode2 == pEnd)
  17. {
  18. break;
  19. }
  20. while (pTmpNode2 != pEnd)
  21. {
  22. if (pTmpNode1->Data > pTmpNode2->Data)
  23. {
  24. TmpData = pTmpNode1->Data;
  25. pTmpNode1->Data = pTmpNode2->Data;
  26. pTmpNode2->Data = TmpData;
  27. }
  28. pTmpNode1 = pTmpNode1->pNext;
  29. pTmpNode2 = pTmpNode2->pNext;
  30. }
  31. pEnd = pTmpNode1;
  32. }
  33. return 0;
  34. }

7、链表的选择排序

  1. //选择排序
  2. int SelectSortLinkList(LinkNode *pHead)
  3. {
  4. LinkNode *pTmpNode = NULL;
  5. LinkNode *pMinNode = NULL;
  6. LinkNode *pSwapNode = NULL;
  7. DataType TmpData;
  8. if (NULL == pHead->pNext)
  9. {
  10. return 0;
  11. }
  12. pSwapNode = pHead->pNext;
  13. while (pSwapNode->pNext != NULL)
  14. {
  15. pMinNode = pSwapNode;
  16. pTmpNode = pSwapNode->pNext;
  17. while (pTmpNode != NULL)
  18. {
  19. if (pTmpNode->Data < pMinNode->Data)
  20. {
  21. pMinNode = pTmpNode;
  22. }
  23. pTmpNode = pTmpNode->pNext;
  24. }
  25. if (pMinNode != pSwapNode)
  26. {
  27. TmpData = pMinNode->Data;
  28. pMinNode->Data = pSwapNode->Data;
  29. pSwapNode->Data = TmpData;
  30. }
  31. pSwapNode = pSwapNode->pNext;
  32. }
  33. return 0;
  34. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/1022076
推荐阅读
相关标签
  

闽ICP备14008679号