当前位置:   article > 正文

线性表的创建与操作_如何创建线性表

如何创建线性表
  1. #include<iostream>
  2. const int InitSize = 100; //目标空间大小
  3. const int MaxSize = 100; //最大空间大小
  4. typedef int Elemtype;
  5. typedef struct {
  6. Elemtype *data;
  7. int length;
  8. }SqList;
  9. //插入
  10. bool ListInsert(SqList &L, int i, Elemtype e) {
  11. if (i<1 || i>L.length + 1) //判断i的值是否有效
  12. return false;
  13. if (L.length >= MaxSize) //存储空间已满
  14. return false;
  15. for (int j = L.length; j >= i; j--) //将第i个元素之后的元素后移
  16. L.data[j] = L.data[j - 1];
  17. L.data[i - 1] = e;
  18. L.length++;
  19. return true;
  20. }
  21. //删除
  22. bool ListDelete(SqList &L, int i, Elemtype &e) {
  23. if (i<1 || i>L.length) //判断i的值是否有效
  24. return false;
  25. e = L.data[i - 1]; //将第i个元素赋值给e
  26. for (int j = i; j < L.length; j++) //第i个元素后的元素前移
  27. L.data[j - 1] = L.data[j];
  28. L.length--;
  29. return true;
  30. }
  31. //输出
  32. void printList(SqList &L) {
  33. for (int i = 0; i < L.length; i++) {
  34. printf("%4d", L.data[i]);
  35. }
  36. printf(&
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/空白诗007/article/detail/966262
推荐阅读
相关标签
  

闽ICP备14008679号