• 数据结构day3


    顺序表的按位置插入、按位置删除和去重

    main.c

    1. #include "seq_list.h"
    2. int main()
    3. {
    4. seq_p L = create_seq_list();
    5. //printf("%d\n",seq_empty(L));
    6. //printf("%d\n",seq_full(L));
    7. insert_pos(L,1,0);
    8. insert_pos(L,3,1);
    9. insert_pos(L,2,1);
    10. out_put(L);
    11. putchar(10);
    12. del_pos(L,1);
    13. out_put(L);
    14. putchar(10);
    15. insert_pos(L,2,1);
    16. insert_pos(L,3,3);
    17. insert_pos(L,2,4);
    18. insert_pos(L,4,5);
    19. insert_pos(L,2,6);
    20. del(L);
    21. out_put(L);
    22. return 0;
    23. }

    seq_list.c

    1. #include "seq_list.h"
    2. //创建顺序表
    3. seq_p create_seq_list()
    4. {
    5. seq_p L = (seq_p)malloc(sizeof(seq_list));
    6. //在堆区申请一个顺序表的空间
    7. if(L==NULL)
    8. {
    9. printf("空间申请失败\n");
    10. return NULL;
    11. }
    12. L->len = 0; //长度置0
    13. bzero(L,sizeof(L->data)); //给数组置0
    14. return L;
    15. }
    16. //判空
    17. int seq_empty(seq_p L)
    18. {
    19. //1\容错判断
    20. if(L==NULL)
    21. {
    22. return -1; //和判断结果区分开
    23. }
    24. return L->len==0?1:0;
    25. }
    26. //判满
    27. int seq_full(seq_p L)
    28. {
    29. //1\容错判断
    30. //
    31. if(L==NULL)
    32. {
    33. return -1; //和判断结果区分开
    34. }
    35. return L->len==MAX?1:0;
    36. }
    37. //打印顺序表
    38. void out_put(seq_p L)
    39. {
    40. if(L==NULL)
    41. {
    42. return;
    43. }
    44. if(seq_empty(L))
    45. {
    46. printf("表为空\n");
    47. return;
    48. }
    49. //循环打印顺序表
    50. for(int i=0;ilen;i++)
    51. {
    52. printf("%d\n",L->data[i]);
    53. }
    54. }
    55. void insert_pos(seq_p L,datatype value,int pos)
    56. {
    57. if(L==NULL)
    58. {
    59. printf("入参为空,请检查\n");
    60. return;
    61. }
    62. if(seq_full(L))
    63. {
    64. printf("表满\n");
    65. return;
    66. }
    67. if(pos>L->len||pos<0)
    68. {
    69. printf("位置不合理\n");
    70. return;
    71. }
    72. for(int i=pos;ilen;i++)
    73. {
    74. L->data[i+1]=L->data[i];
    75. }
    76. L->data[pos]=value;
    77. L->len++;
    78. }
    79. void del_pos(seq_p L,int pos)
    80. {
    81. if(L==NULL)
    82. {
    83. return;
    84. }
    85. if(seq_empty(L))
    86. {
    87. printf("表为空,无需删除\n");
    88. return;
    89. }
    90. for(int i=pos;ilen;i++)
    91. {
    92. L->data[i]=L->data[i+1];
    93. }
    94. L->len--;
    95. }
    96. void del(seq_p L)
    97. {
    98. if(L==NULL)
    99. {
    100. return;
    101. }
    102. if(seq_empty(L))
    103. {
    104. return;
    105. }
    106. if(L->len==1)
    107. {
    108. printf("只有一个元素\n");
    109. return;
    110. }
    111. for(int i=0;ilen;i++)
    112. {
    113. for(int j=0;jlen;j++)
    114. {
    115. if(i!=j&&L->data[i]==L->data[j])
    116. {
    117. del_pos(L,j);
    118. }
    119. }
    120. }
    121. }

    seq_list.h

    1. #ifndef __SEQ_LIST_H__
    2. #define __SEQ_LIST_H__
    3. #include
    4. #include
    5. #define MAX 7
    6. typedef int datatype;
    7. typedef struct seq_list
    8. {
    9. datatype data[MAX];
    10. int len;
    11. }seq_list,*seq_p;
    12. seq_p create_seq_list();
    13. int seq_full(seq_p L);
    14. int seq_empty(seq_p L);
    15. void out_put(seq_p L);
    16. void insert_pos(seq_p L,datatype value,int pos);
    17. void del_pos(seq_p L,int pos);
    18. void del(seq_p L);
    19. #endif

    运行结果

  • 相关阅读:
    activiti流程变量
    SVG转png图片并下载
    技术贴 | 深度解析 PostgreSQL Protocol v3.0(二)— 扩展查询
    学习贪心算法
    js中的this举例介绍
    windows powershell 将U盘启动盘还原回普通U盘
    DBeaver 导出数据的问题 SQL 错误: jdbc 驱动内部错误 Java heap space
    楼宇臭味传感器
    06.倾情奉献之深入分析SpringBoot自动配置
    202427读书笔记|《猫的自信:治愈系生活哲学绘本》——吸猫指南书,感受猫咪的柔软慵懒与治愈
  • 原文地址:https://blog.csdn.net/weixin_69331128/article/details/136243147