当前位置:   article > 正文

数据结构 / 顺序表操作 / 顺序表尾部删除_顺序表删除表尾元素

顺序表删除表尾元素

1.实现逻辑

只需要把顺序表的长度减1。

2.顺序表尾部删除函数代码

  1. /*
  2. *顺序表尾部删除
  3. * */
  4. int delete_tail(sqlist *list)
  5. {
  6. if(NULL==list || 1==is_list_empty(list))
  7. {
  8. return -1;
  9. }
  10. list->len--;
  11. return 0;
  12. }

3. 完整代码

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #define MAXSIZE 20
  5. typedef int data_type;
  6. typedef struct SQLIST
  7. {
  8. //数据元素
  9. data_type arr[MAXSIZE];
  10. int len;
  11. }sqlist;
  12. sqlist *create();
  13. int append(sqlist *list, data_type element);
  14. int is_list_full(sqlist *list);
  15. int is_list_empty(sqlist *list);
  16. void output(sqlist *list);
  17. int delete_tail(sqlist *list);
  18. int main(int argc, const char *argv[])
  19. {
  20. data_type element;
  21. int n;
  22. puts("> ---create a sqlist---\n");
  23. sqlist *list=create();
  24. if(NULL==list)
  25. {
  26. printf("\tfailed to create sqlist!\n");
  27. return -1;
  28. }
  29. printf("\ta sqlist has been created!\n");
  30. putchar(10);
  31. puts("> ---append elements to list---\n");
  32. printf("\thow many element do you want to append?(max:%d):", MAXSIZE);
  33. scanf("%d", &n);
  34. for(int i=0; i<n; i++)
  35. {
  36. printf("\tplease input No.%d element:", i+1);
  37. scanf("%d", &element);
  38. int ret=append(list, element);
  39. if(0==ret)
  40. {
  41. printf("\tlist: len=%d / max=%d!\n", list->len, MAXSIZE);
  42. }
  43. else
  44. {
  45. printf("\tfailed to append element!\n");
  46. break;
  47. }
  48. putchar(10);
  49. }
  50. puts("> ---output list by traversing---\n");
  51. output(list);
  52. putchar(10);
  53. puts("> --- delete tail element---");
  54. int ret = delete_tail(list);
  55. output(list);
  56. putchar(10);
  57. return 0;
  58. }
  59. /*
  60. *申请成功返回堆区内存首地址
  61. *失败返回NULL
  62. * */
  63. sqlist *create()
  64. {
  65. sqlist* list=(sqlist*)malloc(sizeof(sqlist));
  66. if(NULL==list)
  67. {
  68. return NULL;
  69. }
  70. //顺序表长度清零
  71. list->len=0;
  72. //顺序表数据元素清零
  73. memset(list->arr, 0, sizeof(list->arr));
  74. return list;
  75. }
  76. /*
  77. *满了返回1
  78. *不满返回0
  79. * */
  80. int is_list_full(sqlist *list)
  81. {
  82. return list->len==MAXSIZE ? 1 : 0;
  83. }
  84. /*
  85. *注意要判断顺序表是否已满
  86. *成功返回0
  87. *失败返回-1
  88. * */
  89. int append(sqlist *list, data_type element)
  90. {
  91. if(NULL==list || 1==is_list_full(list))
  92. {
  93. return -1;
  94. }
  95. list->arr[list->len++]=element;
  96. return 0;
  97. }
  98. /*
  99. *判断顺序表是否为空
  100. * */
  101. int is_list_empty(sqlist *list)
  102. {
  103. return list->len == 0 ? 1 : 0;
  104. }
  105. /*
  106. *顺序表遍历
  107. *
  108. * */
  109. void output(sqlist *list)
  110. {
  111. if(NULL==list||1==is_list_empty(list))
  112. {
  113. printf("list is NULL!\n");
  114. return;
  115. }
  116. for(int i=0; i<list->len; i++)
  117. {
  118. printf("%d\t", list->arr[i]);
  119. }
  120. putchar(10);
  121. }
  122. /*
  123. *顺序表尾部删除
  124. * */
  125. int delete_tail(sqlist *list)
  126. {
  127. if(NULL==list || 1==is_list_empty(list))
  128. {
  129. return -1;
  130. }
  131. list->len--;
  132. return 0;
  133. }

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

闽ICP备14008679号