单向链表

本是古典 何须时尚 2022-04-17 20:25 374阅读 0赞

单向链表

  • 单向链表存储空间是不连续的,它有数据和指向下一个节点的首地址组成

在这里插入图片描述


代码示例

list.h

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //节点结构体
  4. typedef struct listnode
  5. {
  6. void* data;
  7. struct listnode* next;
  8. }ListNode;
  9. //链表信息结构体
  10. typedef struct listinfo
  11. {
  12. ListNode* head;
  13. int size;
  14. }ListInfo;
  15. typedef void(*PRINTLISTNODE)(void*);//定义函数指针,给Print函数用,参数void*,返回值void
  16. typedef int (*JUDGEEQUAL)(void*,void*);//Find_list的形参,判断相等函数指针,用户提供实现,传入
  17. ListInfo* Init_List();
  18. void Insert_list(ListInfo* list,int pos,void* data);
  19. void RemoveByPos_list(ListInfo* list,int pos);
  20. int Size_list(ListInfo* list);
  21. int Find_list(ListInfo* list,void* data,JUDGEEQUAL judge_equal);
  22. void* Front_list(ListInfo* list);
  23. void Free_list(ListInfo* list);
  24. void Print(ListInfo* list,PRINTLISTNODE print);

list.c

  1. #include "../include/list.h"
  2. //初始化链表
  3. ListInfo* Init_List()
  4. {
  5. ListInfo* list = (ListInfo*)malloc(sizeof(ListInfo));
  6. list->size=0;
  7. list->head=(ListNode*)malloc(sizeof(ListNode));
  8. list->head->data=NULL;
  9. list->head->next=NULL;
  10. return list;
  11. }
  12. //插入节点
  13. void Insert_list(ListInfo* list,int pos,void* data)
  14. {
  15. if(list == NULL) return;
  16. if(pos<0 || pos>list->size)return;
  17. ListNode* NewNode=(ListNode*) malloc(sizeof(ListNode));
  18. NewNode->data=data;
  19. NewNode->next=NULL;
  20. ListNode* pCurrent = list->head;
  21. for(int i=0; i<pos; i++)
  22. {
  23. pCurrent = pCurrent->next;
  24. }
  25. NewNode->next = pCurrent->next;
  26. pCurrent->next = NewNode;
  27. list->size++;
  28. }
  29. //删除节点
  30. void RemoveByPos_list(ListInfo* list,int pos)
  31. {
  32. if(pos<0 || pos>list->size)
  33. return ;
  34. ListNode* pCurrent = list->head;
  35. for(int i=0; i<pos; i++)
  36. {
  37. pCurrent=pCurrent->next;
  38. }
  39. ListNode* tmp=pCurrent->next;
  40. pCurrent->next=pCurrent->next->next;
  41. free(tmp);
  42. list->size--;
  43. }
  44. //返回链表长度
  45. int Size_list(ListInfo* list)
  46. {
  47. return list->size;
  48. }
  49. //查找节点,找到返回位置
  50. int Find_list(ListInfo* list,void* data)
  51. {
  52. if(list==NULL)return -1;
  53. ListNode* pCurrent=list->head;
  54. for(int i=0; i<list->size; i++)
  55. {
  56. pCurrent=pCurrent->next;
  57. if(judge_equal(pCurrent->data,data1))
  58. {
  59. return i;
  60. }
  61. }
  62. return -1;
  63. }
  64. //返回第一个节点
  65. void* Front_list(ListInfo* list)
  66. {
  67. return list->head->next->data;
  68. }
  69. //释放链表
  70. void Free_list(ListInfo* list)
  71. {
  72. if(list == NULL)exit(1);
  73. ListNode* pCurrent = list->head;
  74. while(pCurrent != NULL)
  75. {
  76. ListNode* tmp =pCurrent->next;
  77. free(pCurrent);
  78. pCurrent = tmp;
  79. }
  80. free(list);
  81. }
  82. //打印链表
  83. void Print(ListInfo* list,PRINTLISTNODE print)
  84. {
  85. if(list == NULL)return;
  86. ListNode* pCurrent = list->head->next;
  87. while(pCurrent != NULL)
  88. {
  89. print(pCurrent->data);
  90. pCurrent = pCurrent->next;
  91. }
  92. }
  93. #include <stdio.h>
  94. #include <stdlib.h>
  95. #include "../include/list.h"
  96. typedef struct PERSON
  97. {
  98. char name[64];
  99. int age;
  100. int score;
  101. }Person;
  102. void MyPrint(void* data)
  103. {
  104. Person* p =(Person*)data;
  105. printf("name: %s,age: %d,score: %d\n",p->name,p->age,p->score);
  106. }
  107. int Judge_Equal(void* data1,void* data2)
  108. {
  109. Person* p1 =(Person*)data1;
  110. Person* p2 =(Person*)data2;
  111. if(p1->age==p2->age && p1->name==p2->name && p1->score == p2->score)
  112. {
  113. return 1;
  114. }
  115. else
  116. {
  117. return 0;
  118. }
  119. }
  120. int main(void)
  121. {
  122. ListInfo* list = Init_List();
  123. Person p1 ={ "julian",18,90};
  124. Person p6 ={ "kerr",12,60};
  125. Person p5 ={ "mike",13,30};
  126. Person p4 ={ "john",13,93};
  127. Person p3 ={ "lucy",17,90};
  128. Person p2 ={ "candy",17,80};
  129. Insert_list(list,0,&p1);
  130. Insert_list(list,1,&p6);
  131. Insert_list(list,2,&p2);
  132. Insert_list(list,3,&p3);
  133. Insert_list(list,4,&p4);
  134. Insert_list(list,5,&p5);
  135. Print(list,MyPrint);
  136. printf("----------删除第2个元素---------\n");
  137. RemoveByPos_list(list,2);
  138. Print(list,MyPrint);
  139. printf("--------返回第一个元素-----------\n");
  140. Person* tmp= (Person*)Front_list(list);
  141. printf("name: %s,age: %d,score: %d\n",tmp->name,tmp->age,tmp->score);
  142. printf("---------查找p3,lucy------------------");
  143. int position = Find_list(list,&p3,Judge_Equal);
  144. printf("pos: %d\n",position);
  145. Free_list(list);
  146. return 0;
  147. }

结果

  1. name: julian,age: 18,score: 90
  2. name: kerr,age: 12,score: 60
  3. name: candy,age: 17,score: 80
  4. name: lucy,age: 17,score: 90
  5. name: john,age: 13,score: 93
  6. name: mike,age: 13,score: 30
  7. --------删除第二个元素-----------
  8. name: julian,age: 18,score: 90
  9. name: kerr,age: 12,score: 60
  10. name: lucy,age: 17,score: 90
  11. name: john,age: 13,score: 93
  12. name: mike,age: 13,score: 30
  13. ---------返回第一个元素---------
  14. name: julian,age: 18,score: 90
  15. ---------查找p3,lucy------------------
  16. pos: 2

发表评论

表情:
评论列表 (有 0 条评论,374人围观)

还没有评论,来说两句吧...

相关阅读

    相关 单向

    单向链表也叫单链表,是链表中最简单的一种形式,它的每个节点包含两个域,一个信息域(元素域)和一个链接域。这个链接指向链表中的下一个节点,而最后一个节点的链接域则指向一个空值。

    相关 单向

    一:单向链表 单向链表中的节点包含数据域和next指针域。 ![20210306162342140.png][] 单向链表的增删查改代码实现 Hero