折纸SEO SEO优化 求数据结构算法的源代码,要线性表的,哪有网

求数据结构算法的源代码,要线性表的,哪有网

线性表好像很容易实现吧。 public class HighArray { private long[] ar…

求数据结构算法的源代码,要线性表的,哪有网

线性表好像很容易实现吧。
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(“”);
}
}

本文来自网络,不代表折纸SEO立场,转载请注明出处:https://www.30th-feb.com/4924

作者: DAR_KING

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

联系我们

在线咨询: QQ交谈

邮箱: luckiestmjt@163.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部