当前位置:   article > 正文

数据机构-2(顺序表)

数据机构-2(顺序表)

线性表

概念

顺序表

示例:创建一个存储学生信息的顺序表

表头(Tlen总长度, Clen当前长度)

函数

#include <seqlist.c>

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "seqlist.h"
  4. #include <string.h>
  5. SeqList *CreateSeqList(int len)
  6. {
  7. SeqList *s1 = (SeqList *)malloc(sizeof(SeqList));
  8. if(NULL == s1)
  9. {
  10. perror("Create SeqList malloc fail");
  11. return NULL;
  12. }
  13. s1->head = (DATATYPE *)malloc(sizeof(DATATYPE)*len);
  14. if(NULL == s1->head)
  15. {
  16. perror("create sqlist head malloc");
  17. return NULL;
  18. }
  19. s1->tlen = len;
  20. s1->clen = 0;
  21. return s1;
  22. }
  23. int IsFullSeqList(SeqList *list)
  24. {
  25. return list->clen == list->tlen;
  26. }
  27. int InsertTailSeqList(SeqList *list, DATATYPE *data)
  28. {
  29. if(IsFullSeqList(list))
  30. {
  31. return -1;
  32. }
  33. memcpy(&list->head[list->clen], data, sizeof(DATATYPE));
  34. ++list->clen;
  35. return 0;
  36. }
  37. int get_size_seqlist(SeqList *list)
  38. {
  39. return list->clen;
  40. }
  41. int ShowSeqList(SeqList *list)
  42. {
  43. int len = get_size_seqlist(list);
  44. int i = 0 ;
  45. for(i = 0; i < len; ++i)
  46. {
  47. printf("%s %c %d %d\n", list->head[i].name, list->head[i].sex, list->head[i].age, list->head[i].score);
  48. }
  49. return 0;
  50. }
  51. int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos)
  52. {
  53. if(IsFullSeqList(list) || pos > list->clen)
  54. {
  55. perror("insert fail");
  56. return -1;
  57. }
  58. int i = 0;
  59. for(i = list->clen; pos<i; --i)
  60. {
  61. list ->head[i] = list->head[i-1];
  62. }
  63. memcpy(&list->head[pos], data, sizeof(DATATYPE));
  64. list->clen++;
  65. return 0;
  66. }
  67. int FindSeqList(SeqList *list, char *name)
  68. {
  69. int len = get_size_seqlist(list);\
  70. int i = 0;
  71. for(i = 0; i < len; ++i)
  72. {
  73. if(0 == strcmp(list->head[i].name, name))
  74. {
  75. return i;
  76. }
  77. }
  78. return -1;
  79. }
  80. DATATYPE* get_item_seqlist(SeqList *list, int pos)
  81. {
  82. if(pos < 0 || pos >= list->clen)
  83. {
  84. return NULL;
  85. }
  86. return &list->head[pos];
  87. }
  88. int ModifySeqList(SeqList *list, char *name, DATATYPE *newdata)
  89. {
  90. int ret = FindSeqList(list, name);
  91. if(-1 == ret)
  92. {
  93. return -1;
  94. }
  95. memcpy(&list->head[ret], newdata, sizeof(DATATYPE));
  96. return 0;
  97. }
  98. int ClearSeqList(SeqList *list)
  99. {
  100. list->clen = 0;
  101. return 0;
  102. }
  103. int DestroySeqList(SeqList *list)
  104. {
  105. free(list->head);
  106. free(list);
  107. return 0;
  108. }
  109. int DeleteSeqList(SeqList *list, char *name)
  110. {
  111. int ret = FindSeqList(list, name);
  112. if(ret < 0)
  113. {
  114. printf("not find");
  115. return -1;
  116. }
  117. int i = ret;
  118. for(; i < list->clen; i++)
  119. {
  120. memcpy(&list->head[i+1], &list->head[i], sizeof(list->head[0]));
  121. }
  122. list->clen--;
  123. }

#include “seqlist.h”

  1. #ifndef _SEQLIST_H
  2. #define _SEQLIST_H
  3. typedef struct person{
  4. char name[32];
  5. char sex;
  6. int age;
  7. int score;
  8. }DATATYPE;
  9. typedef struct list{
  10. DATATYPE *head;
  11. int tlen;
  12. int clen;
  13. }SeqList;
  14. SeqList *CreateSeqList(int len);
  15. int DestroySeqList(SeqList *list);//free
  16. int ShowSeqList(SeqList *list);//show
  17. int InsertTailSeqList(SeqList *list, DATATYPE *data);//写
  18. int IsFullSeqList(SeqList *list);//clen ?= tlen
  19. int IsEmptySeqList(SeqList *list);
  20. int InsertPosSeqList(SeqList *list, DATATYPE *data, int pos);//插入
  21. int FindSeqList(SeqList *list, char *name);//find
  22. int ModifySeqList(SeqList *list, char *old, DATATYPE *new);//修改
  23. int DeleteSeqList(SeqList *list, char *name);//删除某
  24. int ClearSeqList(SeqList *list);//clear all
  25. int get_size_seqlist(SeqList *list);//clen = ?
  26. DATATYPE *get_item_seqlist(SeqList *list, int pos);//get head[?]
  27. #endif

主函数

  1. #include <stdio.h>
  2. #include "seqlist.h"
  3. int main()
  4. {
  5. DATATYPE data[] = {
  6. {"jack", 'm', 20, 90},
  7. {"tom", 'm', 21, 78},
  8. {"tony", 'f', 19, 42},
  9. {"sora", 'f', 23, 74},
  10. {"amy", 'f', 21, 69}
  11. };
  12. SeqList *s1 = CreateSeqList(10);
  13. InsertTailSeqList(s1, &data[0]);
  14. InsertTailSeqList(s1, &data[1]);
  15. InsertTailSeqList(s1, &data[2]);
  16. ShowSeqList(s1);
  17. printf("----------------------------------------\n");
  18. InsertPosSeqList(s1, &data[4], 2);
  19. ShowSeqList(s1);
  20. printf("----------------------------------------\n");
  21. int ret = FindSeqList(s1, "tony");
  22. if(-1 == ret)
  23. {
  24. printf("not find\n");
  25. }
  26. else
  27. {
  28. DATATYPE *tmp = get_item_seqlist(s1, ret);
  29. printf("%s %d\n", tmp->name, tmp->score);
  30. }
  31. printf("----------------------------------------\n");
  32. ModifySeqList(s1, "lisi", &data[4]);
  33. ShowSeqList(s1);
  34. printf("----------------------------------------\n");
  35. DeleteSeqList(s1, "tony");
  36. ShowSeqList(s1);
  37. ClearSeqList(s1);
  38. DestroySeqList(s1);
  39. return 0;
  40. }

内存泄漏检测

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

闽ICP备14008679号