• 实现顺序表


    1.顺序表的底层是数组。(既然顺序表的底层是数组,那你要顺序表干嘛?就用数组不行吗?)

    当前这个数组中有多少个有效数据,不要自己数,让程序来。

    实际上数组没有赋值的时候,默认是0,所以此刻数组应该是这样的:

     错误示例:(当数组元素为0时候就跳出循环,并且打印count)

    1. public class Test1 {
    2. public static void main(String[] args) {
    3. int count = 0;
    4. int[] arr = new int[10];
    5. for (int i = 0; i < 3; i++) {
    6. arr[i] = i + 1;
    7. }
    8. for (int i : arr) {
    9. System.out.print(i + " ");
    10. }
    11. //计算一下有效数据的个数
    12. for (int i = 0; i < arr.length; i++) {
    13. if (arr[i] != 0) {
    14. count++;
    15. }
    16. if (arr[i] == 0) {
    17. break;
    18. }
    19. }
    20. System.out.println();
    21. System.out.println("有效数据个数为:" + count);
    22. }
    23. }

    那么问题又来了:

    这样的话,有效数据是6。上面的代码就错了,有效数据还是3。 

    正确的做法,放入一个计数一个!(这样的话,编码比较繁琐,10个数的话还好说,如何数据量大的话,就靠数组难实现,这时候体现出数据结构的重要性,这是一个造轮子的过程,很多函数可以自己实现,比较灵活,轮子造好后,我们就可以重复使用)

    我们先不用顺序表实现一下上面的例子:

    1. public class Test1 {
    2. public static void main(String[] args) {
    3. int count = 0;
    4. int[] arr = new int[10];
    5. for (int i = 0; i < 3; i++) {
    6. count++;
    7. arr[i] = i + 1;
    8. }
    9. arr[5] = 3;
    10. count++;
    11. for (int i : arr) {
    12. System.out.print(i + " ");
    13. }
    14. System.out.println();
    15. System.out.println("有效数据个数为:" + count);
    16. }
    17. }
    18. //打印
    19. //1 2 3 0 0 3 0 0 0 0
    20. //有效数据个数为:4
    21. //

    最后我们先实现一下顺序表,在解决上述问题。

    1. import java.util.Arrays;
    2. public class MyArrayList {
    3. private int[] elem;
    4. private int usedSize;
    5. private static int capacity = 10;
    6. public MyArrayList() {
    7. this.elem = new int[capacity];
    8. }
    9. public boolean isFull() {
    10. if (this.usedSize == capacity) {
    11. return true;
    12. }
    13. return false;
    14. }
    15. public void add(int pos, int data) {
    16. if (pos < 0 || pos > this.usedSize) {
    17. System.out.println("pos位置不合法");
    18. return;
    19. }
    20. if (isFull()) {
    21. this.elem = Arrays.copyOf(this.elem, 2 * capacity);
    22. capacity *= 2;
    23. }
    24. for (int i = this.usedSize - 1; i >= pos; i--) {
    25. this.elem[i + 1] = this.elem[i];
    26. }
    27. this.elem[pos] = data;
    28. this.usedSize++;
    29. }
    30. public void display() {
    31. for (int i = 0; i < this.usedSize; i++) {
    32. System.out.print(this.elem[i] + " ");
    33. }
    34. System.out.println();
    35. }
    36. public boolean isEmpty() {
    37. if (this.usedSize == 0) {
    38. return true;
    39. }
    40. return false;
    41. }
    42. public boolean contains(int toFind) {
    43. if (isEmpty()) {
    44. return false;
    45. }
    46. for (int i = 0; i < this.usedSize; i++) {
    47. if (this.elem[i] == toFind) {
    48. return true;
    49. }
    50. }
    51. return false;
    52. }
    53. public int search(int toFind) {
    54. if (isEmpty()) {
    55. return -1;
    56. }
    57. for (int i = 0; i < this.usedSize; i++) {
    58. if (this.elem[i] == toFind) {
    59. return i;
    60. }
    61. }
    62. return -1;
    63. }
    64. public int getPos(int pos) {
    65. if (isEmpty()) {
    66. throw new RuntimeException("顺序表为空!");
    67. }
    68. if (pos < 0 || pos >= this.usedSize) {
    69. throw new RuntimeException("pos不合法");
    70. }
    71. return this.elem[pos];
    72. }
    73. public int size() {
    74. return this.usedSize;
    75. }
    76. public void setPos(int pos, int value) {
    77. if (pos < 0 || pos >= this.usedSize) {
    78. System.out.println("pos位置不合法!");
    79. return;
    80. }
    81. this.elem[pos] = value;
    82. }
    83. public void remove(int toRemove) {
    84. if (isEmpty()) {
    85. return;
    86. }
    87. int index = search(toRemove);
    88. if (index == -1) {
    89. System.out.println("没有你要删除的数字!");
    90. }
    91. for (int i = index; i < this.usedSize - 1; i++) {
    92. this.elem[i] = this.elem[i + 1];
    93. }
    94. this.usedSize--;
    95. }
    96. public void clear() {
    97. for (int i = 0; i < this.usedSize; i++) {
    98. this.elem[i] = 0;
    99. }
    100. this.usedSize = 0;
    101. }
    102. public static void main(String[] args) {
    103. MyArrayList myArrayList = new MyArrayList();
    104. myArrayList.add(0, 1);
    105. myArrayList.add(1, 2);
    106. myArrayList.add(2, 3);
    107. myArrayList.add(3, 4);
    108. System.out.println(myArrayList.size());
    109. myArrayList.display();
    110. System.out.println(myArrayList.contains(3));
    111. System.out.println(myArrayList.contains(2));
    112. System.out.println(myArrayList.search(5));
    113. System.out.println(myArrayList.search(2));
    114. System.out.println(myArrayList.getPos(0));
    115. System.out.println(myArrayList.usedSize);
    116. myArrayList.display();
    117. myArrayList.remove(1);
    118. myArrayList.remove(2);
    119. myArrayList.display();
    120. myArrayList.remove(4);
    121. myArrayList.display();
    122. myArrayList.clear();
    123. System.out.println("==============");
    124. myArrayList.display();
    125. }
    126. }

    使用myArrayList.usedSize即可解决上述问题。

    顺序表的 增 删 查 改 的时间复杂度:

    增:O  (N)

    删:O  (N)

    查:  (给定关键字) O (N)        (给定下标)O  (1)

    改:(给定下标)   O  (1)

    顺序表的一些缺点:

    增容需要申请新空间,拷贝数据,释放旧空间,会有不小的消耗。

    增容一般是2倍增长,势必会有一定的空间浪费。

    链表会解决上述问题。

  • 相关阅读:
    网络安全(黑客)自学
    合并二叉树(力扣617)
    你不知道并且没听说过的js原生网页共享接口
    简单评分程序(面向对象程序设计及C++)
    python-批量操作excel
    strlen函数使用与模拟实现【进阶版】
    Java记录1:Sublime Text写HelloWrold的常见报错
    关于 vim - YouCompleteMe 的安装
    倍福使用AdsRemote组件实现和C#的ADS通讯
    7月11日学习打卡,数据结构栈
  • 原文地址:https://blog.csdn.net/qq_56444564/article/details/127452611