【数据结构】单链表的基本操作

前端之家收集整理的这篇文章主要介绍了【数据结构】单链表的基本操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、单链表基本概念

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素

二、单链表的基础用法

这里我们先讲一些简单的、基础的用法

如初始化,销毁,插入元素,求链表长度,打印链表以及链表的销毁

除此之外,链表还可以有查找元素,指定位置插入,指定位置删除用法

三、代码

各部分的单独代码块:

结构体:

  1. typedef struct Node
  2. {
  3. DataType data;
  4. struct Node *Next;
  5. }LNode,*pNode,*LinkList;

链表初始化:

  1. int InitList(LinkList *head)//链表的初始化,h为头指针
  2. {
  3. assert(head);
  4. *head=(LinkList)malloc(sizeof(LNode));//动态内存申请
  5. if(!head)
  6. {
  7. printf("Error!");
  8. return 1;
  9. }
  10. return 0;
  11. }

根据位置插入元素:

  1. int InsertList(LinkList head,int nPos,int data)//根据位置插入一个节点
  2. {
  3. assert(head);
  4. pNode cur = head,q;
  5. int i = 0;
  6. while(cur && i < nPos-1)
  7. {
  8. cur = cur->Next;
  9. i++;
  10. }
  11. if(!cur||i>nPos)
  12. {
  13. return 1;
  14. }
  15. q = (pNode)malloc(sizeof(LNode));
  16. if(!q)
  17. {
  18. return 2;
  19. }
  20. q->data = data;
  21. cur->Next = NULL;
  22. q->Next = cur->Next;
  23. cur->Next = q;
  24. return 0;
  25. }

求链表的长度:

  1. int ListLength(LinkList head)//求链表的长度
  2. {
  3. assert(head);
  4. pNode cur=head;
  5. int total=0;
  6. while(cur)//当节点不为空
  7. {
  8. cur = cur->Next;//指向下一个节点
  9. total++;
  10. }
  11. return total;//返回链表的长度
  12. }

打印链表:

  1. void TraveList(LinkList head)//遍历链表
  2. {
  3. assert(head);
  4. pNode cur = head->Next;
  5. while(cur != NULL)
  6. {
  7. printf("\t %d\n",cur->data);
  8. cur = cur->Next;
  9. }
  10. }

链表的销毁:

  1. void DestroyList(LinkList head)
  2. {
  3. assert(head);
  4. pNode cur;
  5. cur = head->Next;
  6. while (cur != NULL)
  7. {
  8. pNode del = cur;
  9. cur = cur->Next;
  10. free(del);
  11. del = NULL;
  12. }
  13. head->Next = NULL;
  14. }


整体代码块以及main函数测试:

  1. #include<stdio.h>
  2. #include<assert.h>
  3. #include"malloc.h"
  4. #define DataType int
  5. typedef struct Node
  6. {
  7. DataType data;
  8. struct Node *Next;
  9. }LNode,*LinkList;
  10.  
  11. int InitList(LinkList *head)//链表的初始化,h为头指针
  12. {
  13. assert(head);
  14. *head=(LinkList)malloc(sizeof(LNode));//动态内存申请
  15. if(!head)
  16. {
  17. return 1;
  18. }
  19. return 0;
  20. }
  21. int ListLength(LinkList head)//求链表的长度
  22. {
  23. assert(head);
  24. pNode cur=head;
  25. int total=0;
  26. while(cur)//当节点不为空
  27. {
  28. cur = cur->Next;//指向下一个节点
  29. total++;
  30. }
  31. return total;//返回链表的长度
  32. }
  33. int InsertList(LinkList head,q;
  34. int i = 0;
  35. while(cur && i < nPos-1)
  36. {
  37. cur = cur->Next;
  38. i++;
  39. }
  40. if(!cur||i>nPos)
  41. {
  42. return 1;
  43. }
  44. q = (pNode)malloc(sizeof(LNode));
  45. if(!q)
  46. {
  47. return 2;
  48. }
  49. q->data = data;
  50. cur->Next = NULL;
  51. q->Next = cur->Next;
  52. cur->Next = q;
  53. return 0;
  54. }
  55. void TraveList(LinkList head)//遍历链表
  56. {
  57. assert(head);
  58. pNode cur = head->Next;
  59. while(cur != NULL)
  60. {
  61. printf("\t %d\n",cur->data);
  62. cur = cur->Next;
  63. }
  64. }
  65.  
  66. void DestroyList(LinkList head)
  67. {
  68. assert(head);
  69. pNode cur;
  70. cur = head->Next;
  71. while (cur != NULL)
  72. {
  73. pNode del = cur;
  74. cur = cur->Next;
  75. free(del);
  76. del = NULL;
  77. }
  78. head->Next = NULL;
  79. }
  80. int main()
  81. {
  82. pNode p;
  83. LinkList h;
  84. InitList(&h);
  85. int count,i,x;
  86. p=(pNode)malloc(sizeof(LNode));
  87. printf("请输入需要插入链表的数据个数:");
  88. scanf("%d",&count);
  89. for(i=0;i<count;i++)
  90. {
  91. printf("请输入第%d个元素的数据:",i+1);
  92. scanf("%d",&x);
  93. InsertList(h,i+1,x);
  94. }
  95. TraveList(h);
  96. DestroyList(h);
  97. TraveList(h);
  98. return 0;
  99. }

运行结果:

------------------->>>assert函数详解!

------------------->>>单链表的其他功能

猜你在找的数据结构相关文章