经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言实现单链表的基本功能详解
来源:jb51  时间:2021/11/24 19:10:26  对本文有异议

1.首先简单了解一下链表的概念:

要注意的是链表是一个结构体实现的一种线性表,它只能从前往后,不可以从后往前(因为next只保存下一个节点的地址).在实现单链表的操作时,需要用指针来操作.很简单,注释写的很详细,欢迎大家指正哈哈哈哈~之前写的太烂了重新写了一下.....

2.代码展示:

  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. typedef struct linklist {
  5. int data;
  6. struct linklist* next;
  7. }node;
  8. //目录
  9. //1.动态申请节点
  10. node* Creatnode(int x);
  11. //2.单链表的尾插
  12. void PushBack(node** plist, int x);
  13. //3.单链表的打印
  14. void Printlist(node** plist);
  15. //4.单链表尾删
  16. void Popback(node** plist);
  17. //5.单链表的头插
  18. void PushFront(node** plist, int x);
  19. //6.单链表的头删
  20. void PopFrount(node** plist);
  21. //7.单链表的查找
  22. node* Findpos(node* plist, int x);
  23. //8.单链表在pos位置之后插入x
  24. void Insertlinstafter(node* pos, int x);
  25. //9.单链表删除pos位置之后的元素
  26. void PopPosAfter(node* pos);
  27. //10.单链表的销毁
  28. void Destorylist(node** plist);
  29. //1.动态申请节点
  30. node* Creatnode(int x) {
  31. node* t = (node*)malloc(sizeof(node));
  32. if (t == NULL) {
  33. assert(0);
  34. return NULL;
  35. }
  36. else {
  37. t->next = NULL;
  38. t->data = x;
  39. return t;
  40. }
  41. }
  42. //2.单链表的尾插
  43. void PushBack(node** plist, int x) {
  44. assert(plist);
  45. if (*plist == NULL) {
  46. *plist = Creatnode(x);
  47. }
  48. else {
  49. node* p = *plist;
  50. while (p->next) {
  51. p = p->next;
  52. }
  53. p->next = Creatnode(x);
  54. }
  55. }
  56. //3.单链表的打印
  57. void Printlist(node** plist) {
  58. assert(plist);
  59. node* p =* plist;
  60. while (p) {
  61. printf("%d ", p->data);
  62. p = p->next;
  63. }
  64. }
  65. //4.单链表尾删
  66. void Popback(node** plist) {
  67. assert(plist);
  68. if (*plist == NULL) {
  69. return NULL;
  70. }
  71. node* p = *plist;
  72. node* q = NULL;
  73. while (p->next) {
  74. q = p;
  75. p = p->next;
  76. }
  77. q->next =NULL;
  78. free(p);
  79. }
  80. //5.单链表的头插
  81. void PushFront(node** plist, int x) {
  82. assert(plist);
  83. node* t = Creatnode(x);
  84. if (NULL == *plist) {
  85. *plist = t;
  86. }
  87. else {
  88. t->next = *plist;
  89. *plist = t;
  90. }
  91. }
  92. //6.单链表的头删
  93. void PopFrount(node** plist) {
  94. assert(plist);
  95. if (plist == NULL) {
  96. return NULL;
  97. }
  98. else {
  99. node* p = *plist;
  100. *plist = p->next;
  101. free(p);
  102. }
  103. }
  104. //7.单链表的查找
  105. node* Findpos(node* plist, int x) {
  106. node* cur = plist;
  107. while (cur) {
  108. if (cur->data == x) {
  109. return cur;
  110. }
  111. cur = cur->next;
  112. }
  113. return NULL;
  114. }
  115. //8.单链表在pos位置之后插入x
  116. void Insertlinstafter(node* pos, int x) {
  117. assert(pos);
  118. if (NULL == pos) {
  119. return ;
  120. }
  121. node* t = Creatnode(x);
  122. t->next = pos->next;
  123. pos->next = t;
  124. }
  125. //9.单链表删除pos位置之后的元素
  126. void PopPosAfter(node* pos) {
  127. assert(pos);
  128. if (pos->next == NULL) {
  129. return;
  130. }
  131. else{
  132. node* p = pos->next;
  133. pos->next = p->next;
  134. free(p);
  135. }
  136. }
  137. //10.单链表的销毁
  138. void Destorylist(node** plist) {
  139. assert(plist);
  140. node* p = *plist;
  141. while (p) {
  142. *plist = p->next;
  143. free(p);
  144. p = *plist;
  145. }
  146. *plist = NULL;
  147. }
  148. void test1() {
  149. node* plist=NULL;//创建头指针
  150. PushBack(&plist, 1);//尾插元素
  151. PushBack(&plist, 2);
  152. PushBack(&plist, 3);
  153. PushBack(&plist, 4);
  154. PushBack(&plist, 5);
  155. Printlist(&plist);//打印链表元素 1 2 3 4 5
  156. printf("\n");
  157. Popback(&plist); //尾删元素
  158. PushFront(&plist, 0);//首插元素0
  159. Printlist(&plist);//打印链表 0 1 2 3 4
  160. printf("\n");
  161. PopFrount(&plist);//首删元素0
  162. Printlist(&plist);//打印链表 1 2 3 4
  163. printf("\n");
  164. Findpos(plist,1);//寻找链表中1的地址,不方便演示,下面会演示
  165. Insertlinstafter(Findpos(plist, 4), 5);//在4后面插入5,用到上面的Findpos函数
  166. Printlist(&plist);//打印链表 1 2 3 4 5
  167. printf("\n");
  168. PopPosAfter(Findpos(plist, 4));//删除指定位置后面的元素(删除4后面的5)
  169. Printlist(&plist);//打印链表 1 2 3 4
  170. printf("\n");
  171. Destorylist(&plist);//销毁链表
  172. Printlist(&plist);//打印链表
  173. }
  174. void test() {
  175. test1();
  176. }
  177. int main() {
  178. test();
  179. return 0;
  180. }

3.测试结果:

a.先创建了头指针plist

b.尾插1 2 3 4 5

c. 尾删元素5

d.首插元素0

e.首删元素0

f.在元素4 后面插入5

g.删除4元素后面的5

h.销毁链表

到此这篇关于C语言实现单链表的基本功能详解的文章就介绍到这了,更多相关单链表基本功能内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号