用Java實現的順序鏈表

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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章