ITeye博客

用Java实现的顺序链表

danlley · 2007-07-22 11:59 · some else · Markdown

java 代码

  1. package org.danlley.util;
  2. /**
  3. * @version 1.0
  4. * @since 2006 年 9 月 17 日     14 : 42 pm
  5. * @author weixm
  6. * 说明:这是一个用数组实现的顺序表,实现了基本的增、删、查操作。
  7. */
  8. public class SequenList {
  9. public int length=10;//初始化顺序表大小为10
  10. private int cur_index;//当前插入位置
  11. private Object[] seqList;//顺序表数据存放处
  12. /**
  13. * @author weixm
  14. * 说明:构造函数,用来初始化顺序表。此处用私有方法来对数据进行初始化。
  15. */
  16. public SequenList(){
  17. initList();
  18. }
  19. /**
  20. * @author weixm
  21. * 初始化顺序表
  22. */
  23. private void initList(){
  24. this.seqList=new Object[length];
  25. }
  26. /**
  27. * @author weixm
  28. * @param obj
  29. * 说明:此方法用来在顺序表中最后位置添加新数据,当数据添加位置超过了数组允许大小时,
  30. *      重新分配内存空间。并且,每加一条数据,List的当前插入位置自增“1”。
  31. */
  32. public void add(Object obj){
  33. if(length>0){
  34. if(cur_index<(length-1)){
  35. seqList[cur_index]=obj;
  36. cur_index++;
  37. }else{
  38. resizeList(seqList);
  39. seqList[cur_index]=obj;
  40. cur_index++;
  41. }
  42. }else{
  43. System.err.print("The sequences are not initialed properly ! ");
  44. }
  45. }
  46. /**
  47. * @author weixm
  48. * @param index
  49. * @param obj
  50. * 说明:此方法用来在数组的任意位置添加一个新数据。如果任意插入的数据位置刚好是最后一个元素
  51. *      则直接调用 add(Object obj) 方法,如果添加的数据超出了原始数据的边界值,则List
  52. *      会自动对空间进行扩充。因此,允许添加数据的当前位置超出数据边界。但是每次插入的数据
  53. *      索引大小不能超过 length*3/2+1 ,否则,系统会抛出数组越界异常。
  54. */
  55. public void add(int index,Object obj){
  56. if(index==cur_index+1){
  57. add(obj);
  58. }else{
  59. int old_len=length;
  60. if(index>length-1){
  61. resizeList(seqList);
  62. }
  63. System.arraycopy(seqList,index,seqList,index+1,old_len-index);
  64. seqList[index]=obj;
  65. cur_index=index;
  66. }
  67. }
  68. /**
  69. * @author weixm
  70. * @param obj
  71. * @return
  72. * 说明:此方法用于进行元素查找,如果要查找的元素在数据中存在,则返回数据所在位置,如果
  73. *      元素在数据中没有找到,则直接返回 -1 。
  74. */
  75. public int findElement(Object obj){
  76. int find_index=-1;
  77. for(int i=0;i
  78. if(obj.equals(seqList[i])){
  79. find_index=i;
  80. }
  81. }
  82. return find_index;
  83. }
  84. /**
  85. * @author weixm
  86. * @param index
  87. * @return
  88. * 说明:此方法用来得到某个确定位置的元素。
  89. */
  90. public Object get(int index){
  91. return seqList[index];
  92. }
  93. /**
  94. * @author weixm
  95. * @param seqList_low
  96. * 说明:重新划分并扩充数据所占内存空间,具体扩充方式为: length*3/2+1 。
  97. */
  98. public void resizeList(Object[] seqList_low){
  99. int resize=length*3/2+1;
  100. Object[] seqList_upp=new Object[resize];
  101. System.arraycopy(seqList_low,0,seqList_upp,0,length);
  102. seqList=seqList_upp;
  103. length=resize;
  104. }
  105. /**
  106. * @author weixm
  107. * @param args
  108. * 说明:测试用例
  109. */
  110. // public static void main(String[] args)throws Exception{
  111. //  try{
  112. //   SequenList list=new SequenList();
  113. //   for(int i=0;i<100;i++){
  114. //    list.add(""+i);
  115. //    //System.out.println("        "+list.get(i));
  116. //   }
  117. //   list.add(133," TEST ");
  118. //   System.out.println(""+list.findElement(""+98));
  119. //   System.out.println(""+list.findElement(" TEST "));
  120. //   System.out.println(""+list.findElement("TEST"));
  121. //   for(int i=0;i
  122. //    System.out.println("        "+list.get(i));
  123. //   }
  124. //   System.out.println("++++++++++++ "+list.length);
  125. //  }catch(Exception e){
  126. //   throw new Exception(e);
  127. //  }
  128. // }
  129. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。