class LineList{
private int[] data; //保存数据元素
private int length;
private int maxlength;
public LineList(int size) {//初始化空链表
data = new int[size];
maxlength = size;
length =0;
}
public void setData(int[] data) {//设置顺序表元素
int i;
for(i=0;i<data.length;i++)
this.data[i]=data[i];
length = data.length;
}
public int getlength() { //获取顺序表长度
return length;
}
public int getNode(int i){//获取指定节点
return data[i];
}
public boolean insert(int e) {//插入节点
int j=0;
for(j=0;j<length;j++) {
if(data[j]==e)
return true;
}
if(length==maxlength) {
System.out.println("error:表已经满!");
return false;
}else {
data[length]=e;
length++;
return true;
}
}
public boolean insert(int i,int e) {//顺序表中指定位置插入数据元素
int j=0;
for (i=0;i<length; i++)
if(data[i]<=e){
return true;
}
for (i=0;i<length;i++){
if(data[i]>e){
for(j=length-1;j>=i;j--){
data[j+1]=data[j];
}
}
data[i]=e;
length++;
return true;
}
}
}
public class MergeLineList {
void merge(LineList LA, LineList LB) {
int i, j, k;
int lenB;
lenB = LB.getlength();
for(i=0;i<lenB;i++) {
LA.insert(LB.getNode(i));
}
}
public static void main(String[] args) {
MergeLineList ml = new MergeLineList();
LineList LA = new LineList(20);
LineList LB = new LineList(20);
int[] a = { 1, 5, 8, 10 };
int[] b = { 3, 5, 9 };
LA.setData(a);
LB.setData(b);
System.out.println("LA数组:"); //输出LA数组
for (int i = 0; i < LA.getlength(); i++)
System.out.print(" " + LA.getNode(i));
System.out.println();
System.out.println("LB数组:"); //输出LB数组
for (int j = 0; j < LB.getlength(); j++)
System.out.print(" " + LB.getNode(j));
ml.merge(LA,LB); //合并LA、LB
System.out.println("合并后LA数组:");
for (int i = 0; i < LA.getlength(); i++)
System.out.print(" " + LA.getNode(i));
}
}
添加回答
举报
0/150
提交
取消