当前位置:   article > 正文

双向循环链表的创建,删除,插入,摧毁等操作(超详细)…… 〃•ω‹〃 (⌯꒪꒫꒪)੭_双向循环链表的插入和删除操作

双向循环链表的插入和删除操作

目录

一.头文件:List.h

 二.函数实现(引入头文件):

1.创建节点

2.打印输出

3.头插与尾插

4.头删与尾删

5.查找节点

6.插入与删除

7.删除链表


引入:带头双向循环链表:结构比较复杂,一般用在单链表存储数据。实际中使用的链表数据结构,都是带头双向循环链表。另外这个结构虽然复杂,但是使用代码实现后就会发现结构会带来很多优势,实现起来反而简单了。 那让我们开始吧~

一.头文件:List.h

  1. #pragma once
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<assert.h>
  5. typedef int LTDataType;//对双向循环链表创建的声明
  6. typedef struct ListNode
  7. {
  8. struct ListNode* next;
  9. struct ListNode* prev;
  10. LTDataType data;
  11. }ListNode;
  12. //创建头结点
  13. ListNode* ListInit();
  14. //打印输出
  15. void ListPrint(ListNode* phead);
  16. //摧毁双向链表
  17. void ListDestory(ListNode* phead);
  18. //头插尾插
  19. void ListPushFront(ListNode* phead, LTDataType x);
  20. void ListPushBack(ListNode* phead, LTDataType x);
  21. //头删尾删
  22. void ListPopFront(ListNode* phead);
  23. void ListPopBack(ListNode* phead);
  24. //查找
  25. ListNode* ListFind(ListNode* phead, LTDataType x);
  26. //插入与删除节点
  27. void ListInsert(ListNode* pos, LTDataType x);
  28. void ListErase(ListNode* pos);

 二.函数实现(引入头文件):

1.创建节点

  1. #include"List.h"
  2. //创建节点
  3. ListNode* ListBuyNode(LTDataType x)
  4. {
  5. ListNode* newnode = (ListNode*)malloc(sizeof(ListNode));
  6. newnode->data = x;
  7. newnode->prev = NULL;
  8. newnode->next = NULL;
  9. return newnode;
  10. }
  11. //创建头结点
  12. ListNode* ListInit()
  13. {
  14. ListNode* phead = ListBuyNode(0);
  15. phead->next = phead;//使节点指向本身,创建双向循环链表的头节点
  16. phead->prev = phead;
  17. return phead;
  18. }

2.打印输出

  1. //打印输出
  2. void ListPrint(ListNode* phead)
  3. {
  4. assert(phead);
  5. ListNode* cur = phead->next;
  6. while (cur != phead)//注意双向循环链表的结束标志不是NULL
  7. {
  8. pirntf("%d", cur->data);
  9. cur = cur->next;
  10. }
  11. printf("\n");
  12. }

3.头插与尾插

尾插

头插

  1. //尾插
  2. void ListPushBack(ListNode* phead, LTDataType x)
  3. {
  4. assert(phead);
  5. ListNode* tail = phead->prev;
  6. ListNode* newnode = ListBuyNode(x);
  7. tail->next = newnode;
  8. newnode->prev = tail;
  9. newnode->next = phead;
  10. phead->prev = newnode;
  11. }
  12. //头插
  13. void ListPushFront(ListNode* phead, LTDataType x)
  14. {
  15. assert(phead);
  16. ListNode* first = phead->next;
  17. ListNode* newnode = ListBuyNode(x);
  18. phead->next = newnode;
  19. newnode->prev = phead;
  20. newnode->next = first;
  21. first->prev = newnode;
  22. }

4.头删与尾删

头删

 尾删

  1. //头删
  2. void ListPopFront(ListNode* phead)
  3. {
  4.     assert(phead);
  5.     ListNode* first = phead->next;
  6.     ListNode* second = first->next;
  7.     phead->next = second;
  8.     second->prev = phead;
  9.     free(first);//防止空指针的出现
  10.     first->next = NULL;
  11. }
  12. //尾删
  13. void ListpopBack(ListNode* phead)
  14. {
  15.     assert(phead);
  16.     assert(phead->next != phead);//防止头结点被删除
  17.     ListNode* tail = phead->prev;
  18.     ListNode* prev = tail->prev;
  19.     prev->next = phead;
  20.     phead->prev = prev;
  21.     free(tail);//防止空指针的出现
  22.     tail->next = NULL;
  23. }

5.查找节点

  1. //查找节点
  2. ListNode* ListFind(ListNode* phead, LTDataType x)
  3. {
  4. assert(phead);
  5. ListNode* cur = phead->next;
  6. while (cur != phead)
  7. {
  8. if (cur->data = x)
  9. {
  10. return cur;
  11. }
  12. cur = cur->next;
  13. }
  14. return NULL;
  15. }

6.插入与删除

 插入

删除

  1. //插入节点
  2. void ListInsert(ListNode* pos, LTDataType x)
  3. {
  4. assert(pos);
  5. ListNode* prev = pos->prev;
  6. ListNode* newnode = ListBuyNode(x);
  7. prev->next = newnode;
  8. newnode->prev = prev;
  9. newnode->next = pos;
  10. pos->prev = newnode;
  11. }
  12. //删除节点
  13. void ListErase(ListNode* pos)
  14. {
  15. assert(pos);
  16. ListNode* prev = pos->prev;
  17. ListNode* next = pos->next;
  18. prev->next = next;
  19. next->prev = prev;
  20. free(pos);
  21. pos->next = NULL;
  22. }

7.删除链表

  1. //删除链表
  2. void LsitDestory(ListNode* phead)
  3. {
  4. assert(phead);
  5. ListNode* cur = phead->next;
  6. while (cur != phead)
  7. {
  8. ListNode* next = cur->next;
  9. free(cur);
  10. cur = next;
  11. }
  12. free(phead);
  13. phead = NULL;
  14. }

博客到这里也是结束了,喜欢的小伙伴可以点赞加关注支持下博主,这对我真的很重要~~ 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/781991
推荐阅读
相关标签
  

闽ICP备14008679号