当前位置:   article > 正文

【数据结构】双链表

【数据结构】双链表

        带头双向循环链表

List.h

  1. #pragma once
  2. #include<stddef.h>//NULL
  3. #include<stdlib.h>//free
  4. #include<assert.h>//assert
  5. typedef int SLDataType;
  6. typedef struct SLNode
  7. {
  8. SLDataType data;
  9. struct SLNode* prev;
  10. struct SLNode* next;
  11. } SLNode;
  12. SLNode* SLInit();
  13. void SLPushFront(SLNode* head, SLDataType x);
  14. void SLPushBack(SLNode* head, SLDataType x);
  15. SLDataType SLPopBack(SLNode* head);
  16. SLDataType SLPopFront(SLNode* head);
  17. SLDataType SLErase(SLNode* Del);
  18. void SLInsertBF(SLNode* AFnode, SLDataType x);
  19. void SLInsertAF(SLNode* AFnode, SLDataType x);
  20. void SLDestroy(SLNode** phead);

List.c

  1. #include"List.h"
  2. static SLNode* BuySLNode(SLDataType x)
  3. {
  4. SLNode* newNode = (SLNode*)malloc(sizeof(SLNode));
  5. if (NULL == newNode)
  6. {
  7. perror("malloc failed");
  8. exit(-1);
  9. }
  10. newNode->data = x;
  11. newNode->prev = newNode;
  12. newNode->next = newNode;
  13. return newNode;
  14. }
  15. SLNode* SLInit()
  16. {
  17. return BuySLNode(0);
  18. }
  19. void SLPushFront(SLNode* head, SLDataType x)
  20. {
  21. assert(head);
  22. //SLInsertBF(head->next, x);
  23. SLInsertAF(head, x);
  24. }
  25. void SLPushBack(SLNode* head, SLDataType x)
  26. {
  27. assert(head);
  28. //SLInsertBF(head, x);
  29. SLInsertAF(head->prev, x);
  30. }
  31. SLDataType SLPopBack(SLNode* head)
  32. {
  33. assert(head);
  34. assert(head->prev != head);
  35. return SLErase(head->prev);
  36. }
  37. SLDataType SLPopFront(SLNode* head)
  38. {
  39. assert(head);
  40. assert(head->next != head);
  41. return SLErase(head->next);
  42. }
  43. SLDataType SLErase(SLNode* Del)
  44. {
  45. assert(Del);
  46. Del->prev->next = Del->next;
  47. Del->next->prev = Del->prev;
  48. SLDataType ret = Del->data;
  49. free(Del);
  50. return ret;
  51. }
  52. void SLInsertBF(SLNode* AFnode, SLDataType x)
  53. {
  54. assert(AFnode);
  55. SLNode* newnode = BuySLNode(x);
  56. AFnode->prev->next = newnode;
  57. newnode->prev = AFnode->prev;
  58. AFnode->prev = newnode;
  59. newnode->next = AFnode;
  60. }
  61. void SLInsertAF(SLNode* BFnode, SLDataType x)
  62. {
  63. assert(BFnode);
  64. SLNode* newnode = BuySLNode(x);
  65. BFnode->next->prev = newnode;
  66. newnode->next = BFnode->next;
  67. BFnode->next = newnode;
  68. newnode->prev = BFnode;
  69. }
  70. void SLDestroy(SLNode** phead)
  71. {
  72. while ((*phead)->next != *phead)
  73. SLPopBack(*phead);
  74. free(*phead);
  75. }

test.c

  1. #include<stdio.h>
  2. #include"List.h"
  3. void print(SLNode* head)
  4. {
  5. SLNode* Head = head;
  6. head = head->next;
  7. printf("Head<->");
  8. while (Head != head)
  9. {
  10. printf("%d<->", head->data);
  11. head = head->next;
  12. }
  13. printf("Head\n");
  14. }
  15. void test1()
  16. {
  17. SLNode* list1 = SLInit();
  18. SLPushBack(list1, 1);
  19. SLPushBack(list1, 2);
  20. SLPushBack(list1, 3);
  21. SLPushBack(list1, 4);
  22. SLPushFront(list1, 11);
  23. SLPushFront(list1, 22);
  24. print(list1);
  25. printf("Del = %d\n", SLPopBack(list1)); print(list1);
  26. printf("Del = %d\n", SLPopFront(list1)); print(list1);
  27. SLDestroy(&list1);
  28. }
  29. int main()
  30. {
  31. test1();
  32. return 0;
  33. }

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

闽ICP备14008679号