当前位置:   article > 正文

数据库--链表(基本操作,尾部插入,定点插入,删除指定,释放全部)_链表与数据库结合

链表与数据库结合

数据结构合集汇总:数据结构合集--汇总--链接

  1. #include <stdio.h>
  2. #include "linklist.h"
  3. //void test_get();
  4. //void test_insert();
  5. int main(int argc, const char *argv[])
  6. {
  7. linklist H;
  8. int value;
  9. H = list_create();
  10. if (H == NULL)
  11. return -1;
  12. printf("input:");
  13. while (1) {
  14. scanf("%d", &value);
  15. if (value == -1)
  16. break;
  17. list_tail_insert(H, value);
  18. printf("input:");
  19. }
  20. //list_show(H);
  21. //list_insert(H,200,3); //an wei zhi cha ru
  22. //list_show(H);
  23. //list_delete(H,3); //shan chu zhiding
  24. //list_show(H);
  25. list_show(H);
  26. printf("H=%p\n", H); //shi fang free ce shi
  27. H = list_free(H);
  28. printf("H=%p\n", H);
  29. list_delete(H,-3);
  30. list_show(H);
  31. list_free(H);
  32. return 0;
  33. }
  34. /*
  35. void test_get() {
  36. linklist H;
  37. int value;
  38. linklist p;
  39. H = list_create();
  40. if (H == NULL)
  41. return;
  42. printf("input:");
  43. while (1) {
  44. scanf("%d", &value);
  45. if (value == -1)
  46. break;
  47. list_tail_insert(H, value);
  48. printf("input:");
  49. }
  50. list_show(H);
  51. p = list_get(H, 4);//1 3 5 7
  52. if (p != NULL)
  53. printf("value=%d\n", p->data);
  54. }
  55. void test_insert() {
  56. linklist H;
  57. int value;
  58. H = list_create();
  59. if (H == NULL)
  60. return;
  61. printf("input:");
  62. while (1) {
  63. scanf("%d", &value);
  64. if (value == -1)
  65. break;
  66. list_tail_insert(H, value);
  67. printf("input:");
  68. }
  69. list_show(H);
  70. list_insert(H, 100, 0);//1 3 5 7
  71. list_show(H);
  72. }*/

 linklist.c  linklist.h

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "linklist.h"
  4. linklist list_create() {
  5. linklist H;
  6. H = (linklist)malloc(sizeof(listnode));
  7. if (H == NULL) {
  8. printf("malloc failed\n");
  9. return H;
  10. }
  11. H->data = 0;
  12. H->next = NULL;
  13. return H;
  14. }
  15. int list_tail_insert(linklist H, data_t value) {
  16. linklist p;
  17. linklist q;
  18. if (H == NULL) {
  19. printf("H is NULL\n");
  20. return -1;
  21. }
  22. //1 new node p
  23. if ((p = (linklist)malloc(sizeof(listnode))) == NULL) {
  24. printf("malloc failed\n");
  25. return -1;
  26. }
  27. p->data = value;
  28. p->next = NULL;
  29. //2 locate locate locate locate locate locate locate locate locate tail node
  30. q = H;
  31. while (q->next != NULL) {
  32. q = q->next;
  33. }
  34. //3 insert
  35. q->next = p;
  36. return 0;
  37. }
  38. linklist list_get(linklist H, int pos) {
  39. linklist p;
  40. int i;
  41. if (H == NULL) {
  42. printf("H is NULL\n");
  43. return NULL;
  44. }
  45. if (pos == -1) {
  46. return H;
  47. }
  48. if (pos < -1) {
  49. printf("pos is invalid\n");
  50. return NULL;
  51. }
  52. p = H;
  53. i = -1;
  54. while (i < pos) {
  55. p = p->next;
  56. if (p == NULL) {
  57. printf("pos is invalid\n");
  58. return NULL;
  59. }
  60. i++;
  61. }
  62. return p;
  63. }
  64. int list_insert(linklist H, data_t value, int pos) {
  65. linklist p;
  66. linklist q;
  67. if (H == NULL) {
  68. printf("H is NULL\n");
  69. return -1;
  70. }
  71. //1 locate node p (pos-1)
  72. p = list_get(H, pos-1);
  73. if (p == NULL) {
  74. return -1;
  75. }
  76. //2 new node q
  77. if ((q = (linklist)malloc(sizeof(listnode))) == NULL) {
  78. printf("malloc failed\n");
  79. return -1;
  80. }
  81. q->data = value;
  82. q->next = NULL;
  83. //3 insert
  84. q->next = p->next;
  85. p->next = q;
  86. return 0;
  87. }
  88. int list_delete(linklist H, int pos) {
  89. linklist p;
  90. linklist q;
  91. //1
  92. if (H == NULL) {
  93. printf("H is NULL\n");
  94. return -1;
  95. }
  96. //2 locate prior
  97. p = list_get(H, pos-1);
  98. if (p == NULL)
  99. return -1;
  100. if (p->next == NULL) {
  101. printf("delete pos is invalid\n");
  102. return -1;
  103. }
  104. //3 update list
  105. q = p->next;
  106. p->next = q->next;//p->next = p->next->next;
  107. //4 free
  108. printf("free:%d\n", q->data);
  109. free(q);
  110. q = NULL;
  111. return 0;
  112. }
  113. int list_show(linklist H) {
  114. linklist p;
  115. if (H == NULL) {
  116. printf("H is NULL\n");
  117. return -1;
  118. }
  119. p = H;
  120. while (p->next != NULL) {
  121. printf("%d ", p->next->data);
  122. p = p->next;
  123. }
  124. puts("");
  125. return 0;
  126. }
  127. linklist list_free(linklist H) {
  128. linklist p;
  129. if (H == NULL)
  130. return NULL;
  131. p = H;
  132. printf("free:");
  133. while (H != NULL) {
  134. p = H;
  135. H = H->next;
  136. printf("%d ", p->data);
  137. free(p);
  138. }
  139. puts("");
  140. return NULL;
  141. }
  1. typedef int data_t;
  2. typedef struct node {
  3. data_t data;
  4. struct node * next;
  5. }listnode, * linklist;
  6. linklist list_create();
  7. int list_tail_insert(linklist H, data_t value);//head
  8. linklist list_get(linklist H, int pos);
  9. int list_insert(linklist H, data_t value, int pos);
  10. int list_delete(linklist H, int pos);
  11. int list_show(linklist H);
  12. linklist list_free(linklist H);

 

 顺序表见:

顺序表操作

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

闽ICP备14008679号