• C++ 顺序线性表的功能


    顺序线性表的功能

    1. //头文件
    2. #pragma once
    3. #define LIST_INIT_SIZE 50
    4. #define LIST_INCREMENT 20
    5. #define OK 1
    6. #define ERROR 0
    7. typedef int Status;
    8. typedef char ElemType;
    9. typedef struct list_ {
    10. ElemType* elem;
    11. int length;
    12. int listize;
    13. }SqList;
    14. // 1 初始化 函数
    15. Status InitList_Sq(SqList& L);
    16. //2 想第i个位置插入数据
    17. Status ListInsert_Sq(SqList& L, int i, ElemType e);
    18. //3 显示所有元素
    19. void PrintAll(SqList L, void print(ElemType));
    20. //4 获取第i个元素的值
    21. Status GetElem(SqList L, int i, ElemType& e);
    22. //5 打印一个元素
    23. void PrintOne(ElemType e);
    24. //删除 第i个位置的元素
    25. Status ListDelete(SqList& L, int i, ElemType& e);
    26. // 用完释放
    27. void DestroyList(SqList& L);
    28. //获取e所在下标
    29. size_t LocateElem(SqList L,ElemType e);
    1. #include
    2. #include
    3. #include "mylist.h"
    4. Status InitList_Sq(SqList& L) {
    5. L.elem = (ElemType*)malloc(sizeof(ElemType) * LIST_INIT_SIZE);
    6. if (!L.elem)
    7. return ERROR;
    8. L.length = 0;
    9. L.listize = LIST_INIT_SIZE;
    10. return OK;
    11. }
    12. Status ListInsert_Sq(SqList& L, int i, ElemType e) {
    13. // i必须大于0 ,小于等于 L.length + 1
    14. if (i<1 || i> L.length + 1)
    15. return ERROR;
    16. // 位置合理,空间不够
    17. if (L.length >= L.listize) {//扩空间
    18. ElemType* newbase = (ElemType*)realloc(L.elem, sizeof(ElemType) * (L.listize + LIST_INCREMENT));
    19. if (!newbase)
    20. exit(-1);
    21. L.elem = newbase;
    22. L.listize += LIST_INCREMENT;
    23. }
    24. ElemType* p;
    25. p = L.elem + (i - 1);
    26. p = &(L.elem[i - 1]);
    27. for (ElemType* q = L.elem + (L.length - 1); q >= p; q--)
    28. *(q + 1) = *q;
    29. *p = e;
    30. L.length += 1;
    31. return OK;
    32. }
    33. void PrintAll(SqList L, void print(ElemType)) {
    34. for (size_t i = 0; i < L.length; i++) {
    35. print(L.elem[i]);
    36. }
    37. printf("\n");
    38. }
    39. Status GetElem(SqList L, int i, ElemType& e) {
    40. // i必须大于0 ,小于等于 L.length
    41. if (i<1 || i> L.length)
    42. return ERROR;
    43. e = L.elem[i - 1]; //e = *(L.elem + i - 1);
    44. return OK;
    45. }
    46. void PrintOne(ElemType e) {
    47. printf("%d ", e);
    48. }
    49. Status ListDelete(SqList& L, int i, ElemType& e) {
    50. // i必须大于0 ,小于等于 L.length
    51. if (i<1 || i> L.length)
    52. return ERROR;
    53. e = L.elem[i - 1]; //e = *(L.elem + i - 1);
    54. for (ElemType* p = L.elem + i - 1; p < L.elem + L.length; p++)
    55. *p = *(p + 1);
    56. L.length--;
    57. return OK;
    58. }
    59. void DestroyList(SqList& L) {
    60. free(L.elem);
    61. L.length = 0;
    62. L.listize = 0;
    63. }
    64. size_t LocateElem(SqList L, ElemType e){
    65. for (size_t i = 0; i < L.length; i++) {
    66. if (L.elem[i] == e) {
    67. return i + 1;
    68. }
    69. }
    70. return 0;
    71. }

    1. #include
    2. #include
    3. #include "mylist.h"
    4. // a = a U b
    5. void unionSet(SqList& a, SqList b);
    6. void unionSet(SqList& a, SqList b) {
    7. //遍历b
    8. for (size_t i = 1; i <= b.length; i++) {//i代表位置,并非下标(从1开始),length不代表下标
    9. ElemType e;
    10. GetElem(b, i,e);
    11. //询问 e 是否在 a集合
    12. if (!LocateElem(a, e)) {
    13. ListInsert_Sq(a, a.length+1, e);
    14. }
    15. }
    16. }
    17. int main() {//2024-5-5
    18. SqList a, b;
    19. InitList_Sq(a);
    20. InitList_Sq(b);
    21. ListInsert_Sq(a, 1, 1);
    22. ListInsert_Sq(a, 2, 2);
    23. ListInsert_Sq(a, 3, 3);
    24. ListInsert_Sq(a, 4, 4);
    25. ListInsert_Sq(a, 5, 5);
    26. ListInsert_Sq(b, 1, 1);
    27. ListInsert_Sq(b, 2, 3);
    28. ListInsert_Sq(b, 3, 9);
    29. ListInsert_Sq(b, 4, 10);
    30. puts("A原来:");
    31. PrintAll(a,PrintOne);
    32. puts("B原来:");
    33. PrintAll(b, PrintOne);
    34. unionSet(a,b);
    35. puts("A现在:");
    36. PrintAll(a, PrintOne);
    37. return 0;
    38. }
    39. int main1() { //2024-5-4
    40. SqList L;
    41. if (!InitList_Sq(L)) {
    42. printf("线性表建立失败,程序退出。");
    43. exit(-1);
    44. }
    45. ListInsert_Sq(L,1,100);
    46. ListInsert_Sq(L, 2, 101);
    47. ListInsert_Sq(L, 3, 102);
    48. ListInsert_Sq(L, 4, 103);
    49. ListInsert_Sq(L, 5, 104);
    50. ListInsert_Sq(L, 6, 105);
    51. ListInsert_Sq(L, 7, 106);
    52. PrintAll(L,PrintOne);
    53. char e;
    54. GetElem(L, 2,e);
    55. PrintOne(e);
    56. printf("\n");
    57. ListDelete(L,2,e);
    58. puts("现在的列表:");
    59. PrintAll(L,PrintOne);
    60. printf("刚才删除的元素是:\n");
    61. PrintOne(e);
    62. printf("\n");
    63. return 0;
    64. }

  • 相关阅读:
    大数据Flink(一百零一):SQL 表值函数(Table Function)
    少数人的晚餐-数据
    3万一5万新能源电动汽车,3万内的新能源电动轿车
    护眼灯值不值得买?护眼灯买哪种好
    Hadoop学习笔记: 分布式数据库 HBase
    【动手学深度学习】卷积神经网络CNN的研究详情
    python与pycharm如何设置文件夹为源代码根目录
    UE4 材质多张图片拼接成一张图片(此处用2×2拼接)
    Springboot 连接 Mysql
    2000-2021年上市公司全要素生产率数据(LP法)(含原始数据、计算代码、计算结果)
  • 原文地址:https://blog.csdn.net/laocooon/article/details/138546053