当前位置:   article > 正文

C数据结构_链表_typedef struct slistnode

typedef struct slistnode

1.定义链表

  1. //无头单向非循环链表
  2. typedef struct SListNode
  3. {
  4. int data;
  5. struct SListNode*next;
  6. }SListNode;
  7. typedef struct SList
  8. {
  9. SListNode*head;
  10. }SList;

2.链表基础功能的实现

//初始化
void Init(SList*p);

//销毁
void Destory(SList*p);

//创建新结点
SListNode *Buy(int x);

//头插
void PushFront(SList*p, int x);

//尾插
void PushBack(SList*p, int x);

//头删
void PopFront(SList*p);

//尾删
void PopBack(SList*p);

//查找
SListNode*Find(SList*p, int x);

//在pos后面插入
void InsertAfter(SListNode*pos, int x);

//在pos后面删除
void EraseAfter(SListNode*pos);

//打印
void Print(SList*p);

  1. //初始化
  2. void Init(SList*p)
  3. {
  4. assert(p);
  5. p->head = NULL;
  6. }
  7. //销毁
  8. void Destory(SList*p)
  9. {
  10. SListNode*p1;
  11. SListNode*cur;
  12. for (cur = p->head; cur != NULL; cur = p1)
  13. {
  14. p1 = cur->next;
  15. free(cur);
  16. }
  17. p->head = NULL;
  18. }
  19. //创建新结点
  20. SListNode *Buy(int x)
  21. {
  22. SListNode*node = (SListNode*)malloc(sizeof(SListNode));
  23. assert(node);
  24. node->data = x;
  25. node->next = NULL;
  26. return node;
  27. }
  28. //头插
  29. void PushFront(SList*p, int x)
  30. {
  31. assert(p);
  32. SListNode*node = Buy(x);
  33. node->next = p->head;
  34. p->head = node;
  35. }
  36. //头删
  37. void PopFront(SList*p)
  38. {
  39. assert(p);
  40. assert(p->head);
  41. SListNode*old_head = p->head;
  42. p->head = p->head->next;
  43. free(old_head);
  44. }
  45. //尾插
  46. void PushBack(SList*p, int x)
  47. {
  48. assert(p);
  49. if (p->head == NULL)
  50. {
  51. PushFront(p, x);
  52. return;
  53. }
  54. SListNode*last = p->head;
  55. while (last->next != NULL)
  56. {
  57. last = last->next;
  58. }
  59. SListNode*node = Buy(x);
  60. last->next = node;
  61. }
  62. //尾删
  63. void PopBack(SList*p)
  64. {
  65. assert(p);
  66. assert(p->head);
  67. if (p->head->next == NULL)
  68. {
  69. PopFront(p);
  70. return;
  71. }
  72. SListNode*cur = p->head;
  73. while (cur->next->next != NULL)
  74. {
  75. cur = cur->next;
  76. }
  77. free(cur->next);
  78. cur->next = NULL;
  79. }
  80. //查找
  81. SListNode*Find(SList*p, int x)
  82. {
  83. SListNode*cur = p->head;
  84. for (; cur != NULL; cur = cur->next)
  85. {
  86. if (cur->data = x)
  87. {
  88. return cur;
  89. }
  90. }
  91. return NULL;
  92. }
  93. //在pos后面插入
  94. void InsertAfter(SListNode*pos, int x)
  95. {
  96. SListNode*node = Buy(x);
  97. node->next = pos->next;
  98. pos->next = node;
  99. }
  100. //在pos后面删除
  101. void EraseAfter(SListNode*pos)
  102. {
  103. SListNode*next = pos->next->next;
  104. free(pos->next);
  105. pos->next = next;
  106. }
  107. //打印
  108. void Print(SList*p)
  109. {
  110. SListNode*cur = p->head;
  111. for (; cur != NULL; cur = cur->next)
  112. {
  113. printf("%d-->", cur->data);
  114. }
  115. printf("NULL\n");
  116. }

 

 

 

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

闽ICP备14008679号