线性表好像很容易实现吧。
public class HighArray {
private long[] arr;
private int nElems; // number of items.
public HighArray(int max){
arr = new long[max];
nElems = 0;
}
//find item.
public boolean find(long searchKey){
int j ;
for(j=0;j<nElems;j++)
if(arr[j] == searchKey)
break;
if(j == nElems)
return false;
else
return true;
} //end find;
//insert a item
public void insert(long value){
arr[nElems] = value;
nElems++;
}
//delete item
public boolean delete(long value){
int j ;
for(j=0;j<nElems;j++)
if(arr[j]==value)
break;
if(j == nElems)
return false;
else{
for(int k=j;k<nElems;k++)
arr[k] = arr[k+1];
nElems–;
return true;
}
}//end delete
public void display(){
for(int i=0;i<nElems;i++)
System.out.print(arr[i] + ” “);
System.out.println(” “);
}
}
二分查找的算法:
public class OrdArray {
private long[] arr;
private int nElems;
public OrdArray(int max){
arr = new long[max];
nElems = 0;
}
public int size(){
return nElems;
}
//find
public int find(long searchKey){
int lowerBound = 0;
int upperBound = nElems – 1;
int curIn;
while(true){
curIn = (lowerBound + upperBound) / 2;
if(arr[curIn] == searchKey)
return curIn; //find, return cur
else if(lowerBound > upperBound)
return nElems; // can’t find,return array of size
else{
if(arr[curIn] < searchKey)
lowerBound = curIn + 1;
else
upperBound = curIn – 1;
}
}
} //end find
//insert
public void insert(long value){
int j ;
for(j=0;j<nElems;j++)
if(arr[j] > value)
break;
for(int k = nElems;k>j;k–)
arr[k] = arr[k-1];
arr[j] = value;
nElems++;
}//end insert
//delete
public boolean delete(long value){
int j = find(value);
if(j == nElems)
return false;
else{
for(int k=j;k<nElems;k++)
arr[k] = arr[k+1];
nElems–;
return true;
}
}//end delete
public void display(){
for(int j=0;j<nElems;j++)
System.out.print(arr[j]+” “);
System.out.println(“”);
}
}