1. /*
    2. * 初始版本java实现顺序链表
    3. * 未考虑线程安全问题
    4. *
    5. * and open the template in the editor.
    6. */
    7. import java.util.Arrays;
    8. /**
    9. *
    10. * @author wyman
    11. */
    12. public class SequenceList<T> implements ListI<T> {
    13. int size = 0;//实际长度
    14. int init_listSize = 100;//初始长度
    15. public Object[] objs;//用来保存数据
    16. public SequenceList(int n) {
    17. this.objs = new Object[n];
    18. this.init_listSize = n;
    19. }
    20. public SequenceList() {
    21. this.objs = new Object[10];
    22. this.init_listSize = 10;
    23. }
    24. //初始化一个顺序表(对应严蔚敏版数据结构用)over
    25. @Override
    26. public ListI initList() {
    27. return new SequenceList<T>(init_listSize);
    28. }
    29. //over
    30. @Override
    31. public void clearList() {
    32. this.size = 0;
    33. }
    34. //over 链表是否为空
    35. @Override
    36. public boolean listEmpty() {
    37. if (this.size == 0) {
    38. return true;
    39. }
    40. return false;
    41. }
    42. //返回compare的位序,不存在返回0
    43. @Override
    44. public int locateElem(T t) {
    45. int index = 0;
    46. for (int i = 0; i < this.objs.length; i++) {
    47. if (this.objs[i].equals(t)) {
    48. index = i+1;
    49. }
    50. }
    51. return index;
    52. }
    53. //
    54. @Override
    55. public T getElem(int i) {
    56. if (i >= 1 || i <= this.size) {
    57. this.size--;
    58. return (T) objs[i-1];
    59. }
    60. new Exception("查询越界").printStackTrace();
    61. return null;
    62. }
    63. @Override
    64. public T priorElem(T t) {
    65. int index = 0;
    66. if (this.size <= 0) {
    67. return null;
    68. }
    69. for (int i = 0; i < this.objs.length; i++) {
    70. if (this.objs[i].equals(t)) {
    71. index = i;
    72. }
    73. }
    74. return (T) this.objs[--index];
    75. }
    76. @Override
    77. public T nextrElem(T t) {
    78. int index = 0;
    79. if (this.size <= 0) {
    80. return null;
    81. }
    82. for (int i = 0; i < this.objs.length; i++) {
    83. if (this.objs[i].equals(t)) {
    84. index = i + 1;
    85. }
    86. }
    87. if (this.size >= index) {
    88. return null;
    89. }
    90. return (T) this.objs[--index];
    91. }
    92. @Override
    93. public boolean listInsert(int i, T t) {
    94. if (i < 1 || i > size + 1) {
    95. return false;
    96. }
    97. if (size == init_listSize) {
    98. //空间满了分配新空间1.5倍
    99. init_listSize = size + (size >> 1);
    100. Object[] new_Arr = Arrays.copyOf(this.objs, size);
    101. this.objs = new_Arr;
    102. }
    103. Object old_element = this.objs[i - 1];
    104. for (int a = i - 1; a < size; a++) {
    105. this.objs[a + 1] = this.objs[a];
    106. }
    107. this.objs[i - 1] = t;
    108. size++;
    109. return true;
    110. }
    111. @Override
    112. public T listDelete(int i) {
    113. if (i < 1 || i > size) {
    114. return null;
    115. }
    116. for (int a = i; a < size; a++) {
    117. this.objs[a - 1] = this.objs[a];
    118. }
    119. size--;
    120. return null;
    121. }
    122. @Override
    123. public void listTraverse() {
    124. }
    125. @Override
    126. public int getSize() {
    127. return this.size;
    128. }
    129. @Override
    130. public void DestoryList(ListI l) {
    131. l=null;
    132. }
    133. }