Java,數據結構,線性表,順序實現

接口:

package com.ghostwolf.ds.linearitytable.order.interfaces;
/**
*
InitList(L)
DestroyList(L)
ClearList(L)
ListEmpty(L)
ListLength(L)
GetElem(L,i,v)
LocateElem(L,e,compare())
PriorElem(L,cur_e,&pre_e)
NextElem(L,cur_e,&next_e)
ListInsert(&L,i,e)
ListDelete(&L,i,&e)
ListTraverse(L,visit())
union(List &La,List &Lb)
*/
public interface List
{
 public void initList();
 public void destroyList();
 public void clearList();
 public boolean listEmpty();
 public int listLength();
 public Object getElem(int index);
 public int locateElem(Object search);
 public Object priorElem(Object current);
 public Object nextElem(Object current);
 public boolean listInsert(Object current,int index);
 public boolean listDelete(int index);
 public boolean listDelete(Object target);
 public Object[] listTraverse(int from,int end);
 public Object[] union(Object[] objectArrayOne,Object[] objectArrayOneTwo,boolean oneFirst);
}
 實現類:

package com.ghostwolf.ds.linearitytable.order;
import com.ghostwolf.ds.linearitytable.order.interfaces.List;
public class OrderTableExtendsList implements com.ghostwolf.ds.linearitytable.order.interfaces.List
{
 private int max=-1;
 private int current=-1;
 private Object[] objectArray;
 public OrderTableExtendsList(){
  max = 100;
  initList();
 }
 public OrderTableExtendsList(int max){
  this.max = max;
  initList();
 }
  public void initList(){
  objectArray = new Object[max];
  current = 0;
 }
 public void destroyList(){
  objectArray = null;
  current = -1;
  max = -1;
 }
 public void clearList(){
  objectArray = new Object[max];
  current = -1;
 }
 public boolean listEmpty(){
  if(current<0){
   return true;
  }
  return false;
 }
 public int listLength(){
  return current;
 }
 public Object getElem(int index){
  if(index >current || index <0){
   return null;
  }
  return objectArray[index];
 }
 public int locateElem(Object search){
  for(int i = 0;i<current;i++){
   if(search.equals(objectArray[i])){
    return i+1;
   }
  }
  return -1;
 }
 public Object priorElem(Object currentObject){
  for(int i = 0;i<current;i++){
   if(currentObject.equals(objectArray[i]) &&  (i-1) >= 0  ){
    return objectArray[i-1];
   }
  }
  return null;
 
 }
 public Object nextElem(Object currentObject){
  for(int i = 0;i<current;i++){
   if(currentObject.equals(objectArray[i]) && (i+1) <=current ){
    return objectArray[i+1];
   }
  }
  return null;
 }
 public boolean listInsert(Object value,int index){
  index = index-1;
  if(index > current || index <0){
   return false;
  }
  int j = current+1;
  while(j>index){
   objectArray[j] =objectArray[j-1];
   j--;
  }
  objectArray[index] = value;
  current++;
  return true;
 }
 public boolean listDelete(int index){
  index = index-1;
  if(index > current || index <0){
   return false;
  }
  while(index<current){
   objectArray[index] =objectArray[index+1];
   index++;
  }
  current--;
  return true;
 }
 public boolean listDelete(Object target){
  int index = locateElem(target);
  return listDelete(index);
 }
 public Object[] listTraverse(int from,int end){
  from = from -1;
  end = end -1;
  if(from>end || from <0 || end >current || from>current || end <0){
   return null;
  }
  Object[] temp = new Object[end - from+1];
  int j = 0;
  for(int i = from;i<=end;i++){
   temp[j] = objectArray[i];
   j++;
  }
  return temp;
 }
 public Object[] union(Object[] objectArrayOne,Object[] objectArrayOneTwo,boolean oneFirst){
  Object[] temp  = new Object[objectArrayOne.length+objectArrayOneTwo.length];
  System.out.println("Temp.length"+temp.length+objectArrayOne.length+objectArrayOneTwo.length);
  if(oneFirst == true){
   int j = 0;
   for(int i = 0;i<temp.length;i++){
    if(i>=objectArrayOne.length){
     temp[i] = objectArrayOneTwo[j];
     j++;
    }else{
     temp[i] = objectArrayOne[i];
    }
   }
  }else{
   int j = 0;
   for(int i = 0;i<temp.length;i++){
    if(i>=objectArrayOneTwo.length){
     temp[i] = objectArrayOne[j];
     j++;
    }else{
     temp[i] = objectArrayOneTwo[i];
    }
   }
  }
  
  return temp;
 }
 public void showAll(){
  if(current>1){
   System.out.println("開始顯示所有");
   for(int i=0;i<current;i++){
    System.err.println(objectArray[i]);
   }
   System.out.println("顯示完成");
  }
 }
 public static void main(String[] args){
  OrderTableExtendsList test = new OrderTableExtendsList();
  System.out.println("添加從1到10");
  for(int i =1;i<=10;i++){
   boolean temp = test.listInsert(new Integer(i),i);
  }
  System.out.println("長度"+test.listLength());
  test.showAll();
  System.out.println("是否爲空"+test.listEmpty());
  System.out.println("刪除第2個是否成功"+test.listDelete(2));
  System.out.println("長度"+test.listLength());
  test.showAll();
  System.out.println("在第8個插入99"+test.listInsert(new Integer(99),8));
  System.out.println("長度"+test.listLength());
  test.showAll();
  System.out.println("查找值爲99的位置爲"+test.locateElem(new Integer(99)));
  System.out.println("查找值爲99的前一個值爲"+test.priorElem(new Integer(99)));
  System.out.println("查找值爲99的後一個值爲"+test.nextElem(new Integer(99)));
  System.out.println("刪除值爲99是否成功"+test.listDelete(new Integer(99)));
  System.out.println("長度"+test.listLength());
  test.showAll();
  System.out.println("從3截取到5");
  Object[] value = test.listTraverse(3,5);
  for(int i =0;i<value.length;i++){
   System.out.println(value[i]);
  }
  System.out.println("從5截取到6");
  Object[] valueT = test.listTraverse(5,6);
  for(int i =0;i<valueT.length;i++){
   System.out.println(valueT[i]);
  }
  Object[] tempValue = test.union(value,valueT,true);
  for(int i =0;i<tempValue.length;i++){
   System.out.println(tempValue[i]);
  }
 }
}

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