当前位置:   article > 正文

C语言--数据结构:单链表

C语言--数据结构:单链表

1、认知

什么是链表,顾名思义就像一个个环环相扣的圈。那我们怎么使用编程语言实现这个链表呢?以及它的作用是什么呢?

单链表是一种线性数据结构,由一系列节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。节点按顺序排列,通过指针链接在一起,形成链表。

单链表的主要作用包括:

  1. 存储数据: 单链表可以用来存储各种类型的数据,例如整数、浮点数、字符串等等。

  2. 动态分配内存: 单链表的节点可以动态分配内存,这意味着它可以根据需要灵活地增加或减少节点,不像数组一样需要在初始化时确定大小。

  3. 插入和删除操作高效: 在单链表中,插入和删除元素的操作非常高效,只需要修改节点的指针即可,无需移动其他元素。

  4. 实现其他数据结构: 单链表可以用作其他数据结构的基础,例如栈、队列等,通过在单链表上实现特定的操作即可实现这些数据结构。

  5. 处理大数据集: 当需要处理大量数据,并且事先无法确定数据量大小时,单链表可以提供一种灵活的解决方案,因为它的大小可以根据需要进行动态调整。

2、原理

上图是链表的一个简图:

  • data是自己定义的数据类型
  • next是指向下一个节点的指针

易知,我们只需要知道第一个节点的地址就可以顺藤摸瓜找到余下节点的地址,所以我们要先创建一个节点。

以下为节点的定义:

  1. typedef int SLTDataType;
  2. typedef struct SListNode
  3. {
  4. SLTDataType date;
  5. struct SListNode* next;
  6. //
  7. }SLTNode;

单链表由节点组成,每个节点包含两部分:数据和指针。数据存储实际数据,指针指向下一个节点。

单链表的基本操作:

  • 创建链表: 动态分配内存创建节点,并将节点链接起来形成链表。

  • 插入节点: 在链表的特定位置或末尾插入新节点。

  • 删除节点: 删除链表中的特定节点。

  • 遍历链表: 通过指针依次访问链表中的每个节点。

  • 查找节点: 在链表中查找特定值的节点。

一、创建链表、插入节点

在主函数中我们创建一个指针节点,然后往链表中插入数据。

  1. int main()
  2. {
  3. SLTNode* head = NULL;
  4. SLTPushBack(&head, 1);
  5. SLTPushBack(&head, 2);
  6. SLTPushBack(&head, 3);
  7. return 0;
  8. }

Push为插入函数插入分头插和尾插,我先讲尾插。

  1. void SLTPushBack(SLTNode** pphead, SLTDataType x)
  2. {
  3. assert(pphead);
  4. SLTNode* pcul =SLTBuy(x);
  5. SLTNode* preal = *pphead;
  6. if (*pphead==NULL)
  7. {
  8. *pphead= pcul;
  9. return;
  10. }
  11. while (preal->next)
  12. {
  13. preal = preal->next;
  14. }
  15. preal->next= pcul;
  16. }

 每当我们插入数据时先调用一个Buy函数创建好节点:

  1. SLTNode* SLTBuy(SLTDataType x)
  2. {
  3. SLTNode* pret = (SLTNode*)malloc(sizeof(SLTNode));
  4. pret->date = x;
  5. pret->next = NULL;
  6. return pret;
  7. }

在尾插时我们分为两种情况,当链表为空时,我们直接将创建好的节点赋值给头节点,链表不为空时,我们就要创建一个指针顺着链表走,走到头节点。while中的preal->next意思是当这个节点的next为NULL时就走出循环,这样我们就得到了指向末尾的一个指针, 然后将创建好的节点赋值给末尾就可以了

头插依旧分为两种情况  链表为空  链表不为空,链表为空也是直接赋值给头节点,不为空就先将创建好的新节点的next指向头节点,然后重新让旧头节点的指针指向新的头节点就可以。

有点绕,理解就好了,以下是头插代码:

  1. void SLTPushFront(SLTNode** pphead, SLTDataType x)
  2. {
  3. assert(pphead);
  4. SLTNode* nownode = SLTBuy(x);
  5. if (*pphead == NULL)
  6. {
  7. *pphead = nownode;
  8. return;
  9. }
  10. nownode->next = *pphead;
  11. *pphead = nownode;
  12. }

二、尾删和头删

先讲尾删,就是先遍历一遍链表然后找到最后一个节点,并将其free就可以,但也分只有一个节点的情况:

  1. void SLTPopBack(SLTNode** pphead)
  2. {
  3. assert(pphead);
  4. assert(*pphead);
  5. SLTNode* pre = *pphead;
  6. SLTNode* ptail = pre->next;
  7. if ((*pphead)->next == NULL)
  8. {
  9. free(*pphead);
  10. *pphead = NULL;
  11. return;
  12. }
  13. while (ptail->next)
  14. {
  15. pre = pre->next;
  16. ptail = pre->next;
  17. }
  18. pre->next = NULL;
  19. free(ptail);
  20. ptail = NULL;
  21. }

头删依旧分两种情况  只有一个节点  和   多个节点;我们只需要删除前一个节点并将头节点向后移动一个节点就可以,但是记得向后移动头节点前先保留要删除节点的指针。

  1. void SLTPopFront(SLTNode** pphead)
  2. {
  3. assert(pphead);
  4. assert(*pphead);
  5. if ((*pphead)->next == NULL)
  6. {
  7. free(*pphead);
  8. *pphead = NULL;
  9. return;
  10. }
  11. SLTNode* nownode = *pphead;
  12. *pphead = (*pphead)->next;
  13. free(nownode);
  14. nownode = NULL;
  15. }

剩余的查找和在指定位置删除节点,在理解了前面链表的定义最后看代码就可以理解,我就直接放出我的代码,希望可以帮你更好的理解链表。

头文件:SList.h

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<assert.h>
  4. typedef int SLTDataType;
  5. typedef struct SListNode
  6. {
  7. SLTDataType date;
  8. struct SListNode* next;
  9. //
  10. }SLTNode;
  11. SLTNode* SLTBuy(SLTDataType x);
  12. void SLTPrint(SLTNode* phead);
  13. //头部插入删除/尾部插入删除
  14. void SLTPushBack(SLTNode** pphead, SLTDataType x);
  15. void SLTPushFront(SLTNode** pphead, SLTDataType x);
  16. void SLTPopBack(SLTNode** pphead);
  17. void SLTPopFront(SLTNode** pphead);
  18. //查找
  19. SLTNode* SLTFind(SLTNode* phead, SLTDataType x);
  20. //在指定位置之前插入数据
  21. void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x);
  22. //删除pos节点
  23. void SLTErase(SLTNode** pphead, SLTNode* pos);
  24. //在指定位置之后插入数据
  25. void SLTInsertAfter(SLTNode* pos, SLTDataType x);
  26. //删除pos之后的节点
  27. void SLTEraseAfter(SLTNode* pos);
  28. //销毁链表
  29. void SListDesTroy(SLTNode** pphead);

源文件SList.c

  1. #include"SList.h"
  2. void SLTPrint(SLTNode* phead) {
  3. SLTNode* pcul = phead;
  4. while (pcul)
  5. {
  6. printf("%d->", pcul->date);
  7. pcul = pcul->next;
  8. }
  9. printf("NULL\n");
  10. }
  11. void SLTPushBack(SLTNode** pphead, SLTDataType x)
  12. {
  13. assert(pphead);
  14. SLTNode* pcul =SLTBuy(x);
  15. SLTNode* preal = *pphead;
  16. if (*pphead==NULL)
  17. {
  18. *pphead= pcul;
  19. return;
  20. }
  21. while (preal->next)
  22. {
  23. preal = preal->next;
  24. }
  25. preal->next= pcul;
  26. }
  27. SLTNode* SLTBuy(SLTDataType x)
  28. {
  29. SLTNode* pret = (SLTNode*)malloc(sizeof(SLTNode));
  30. pret->date = x;
  31. pret->next = NULL;
  32. return pret;
  33. }
  34. void SLTPushFront(SLTNode** pphead, SLTDataType x)
  35. {
  36. assert(pphead);
  37. SLTNode* nownode = SLTBuy(x);
  38. if (*pphead == NULL)
  39. {
  40. *pphead = nownode;
  41. return;
  42. }
  43. nownode->next = *pphead;
  44. *pphead = nownode;
  45. }
  46. void SLTPopBack(SLTNode** pphead)
  47. {
  48. assert(pphead);
  49. assert(*pphead);
  50. SLTNode* pre = *pphead;
  51. SLTNode* ptail = pre->next;
  52. if ((*pphead)->next == NULL)
  53. {
  54. free(*pphead);
  55. *pphead = NULL;
  56. return;
  57. }
  58. while (ptail->next)
  59. {
  60. pre = pre->next;
  61. ptail = pre->next;
  62. }
  63. pre->next = NULL;
  64. free(ptail);
  65. ptail = NULL;
  66. }
  67. void SLTPopFront(SLTNode** pphead)
  68. {
  69. assert(pphead);
  70. assert(*pphead);
  71. if ((*pphead)->next == NULL)
  72. {
  73. free(*pphead);
  74. *pphead = NULL;
  75. return;
  76. }
  77. SLTNode* nownode = *pphead;
  78. *pphead = (*pphead)->next;
  79. free(nownode);
  80. nownode = NULL;
  81. }
  82. SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
  83. {
  84. assert(phead);
  85. while (phead)
  86. {
  87. if ((phead->date == x))
  88. {
  89. return phead;
  90. }
  91. phead = phead->next;
  92. }
  93. return NULL;
  94. }
  95. void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
  96. {
  97. assert(pphead);
  98. assert(*pphead);
  99. assert(pos);
  100. SLTNode* nownode = SLTBuy(x);
  101. SLTNode* pcul = *pphead;
  102. if (pos == *pphead)
  103. {
  104. nownode->next = *pphead;
  105. *pphead = nownode;
  106. return;
  107. }
  108. while (pcul->next != pos)
  109. {
  110. pcul = pcul->next;
  111. }
  112. nownode->next = pcul->next;
  113. pcul->next = nownode;
  114. }
  115. void SLTErase(SLTNode** pphead, SLTNode* pos)
  116. {
  117. assert(pphead);
  118. assert(*pphead);
  119. assert(pos);
  120. SLTNode* nownode = *pphead;
  121. SLTNode* nownode2 = NULL;
  122. if (pos == *pphead)
  123. {
  124. *pphead = (*pphead)->next;
  125. free(nownode);
  126. nownode = NULL;
  127. return;
  128. }
  129. while (nownode->next != pos)
  130. {
  131. nownode = nownode->next;
  132. }
  133. nownode2 = nownode->next;
  134. nownode->next = nownode->next->next;
  135. free(nownode2);
  136. nownode2 = NULL;
  137. }
  138. void SLTInsertAfter(SLTNode* pos, SLTDataType x)
  139. {
  140. assert(pos);
  141. SLTNode* nownode = SLTBuy(x);
  142. nownode->next = pos->next;
  143. pos->next = nownode;
  144. }
  145. void SLTEraseAfter(SLTNode* pos)
  146. {
  147. assert(pos);
  148. assert(pos->next);
  149. SLTNode* nownode = pos->next;
  150. pos->next = pos->next->next;
  151. free(nownode);
  152. nownode = NULL;
  153. }
  154. void SListDesTroy(SLTNode** pphead)
  155. {
  156. assert(pphead);
  157. SLTNode* pre = *pphead;
  158. while (*pphead != NULL)
  159. {
  160. pre = *pphead;
  161. *pphead = (*pphead)->next;
  162. free(pre);
  163. pre = NULL;
  164. }
  165. }

test主函数文件:大家可以自行用函数测试

  1. #include"SList.h"
  2. int main()
  3. {
  4. SLTNode* head =NULL;
  5. SLTPushBack(&head, 1);
  6. SLTPushBack(&head, 2);
  7. SLTPushBack(&head, 3);
  8. SLTPushBack(&head, 4);
  9. SLTPrint(head);
  10. SLTNode* new = SLTFind(head, 2);
  11. SLTInsertAfter(new, 100);
  12. SLTPrint(head);
  13. SLTEraseAfter(new);
  14. SLTPrint(head);
  15. SListDesTroy(&head);
  16. return 0;
  17. }

通过本文的介绍,读者应该对C语言中的单链表有了深入的了解。单链表是一种灵活而强大的数据结构,能够在各种应用场景中发挥重要作用。掌握单链表的概念和操作,将有助于读者更好地理解和应用数据结构与算法

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

闽ICP备14008679号