顺序表一个出,要求:   有顺序表A和B,其元素均按从小到大的升序排列,编写一个算法将它们合并成一个顺序表C,要求C的元素也是从小到大的升序排列。
算法思路:依次扫描通过A和B的元素,比较当前的元素的值,将较小值的元素赋给C,如此直到一个线性表扫描完毕,然后将未完的那个顺序表中余下部分赋给C即可。C的容量要能够容纳A、B两个线性表相加的长度。
//线性表的顺序存储结构package dataStructure.linearList;public class SeqList<E> extends AbstractList<E>     //顺序表类
{
    private Object[] table;                      //对象数组,私有成员
    private int n;                               //顺序表长度
    
    public SeqList(int capacity)                 //构造方法,创建指定容量的空表
    {
        this.table = new Object[Math.abs(capacity)];  //Math.abs(i)返回参数的绝对值
        this.n = 0;
    }
    public SeqList()                             //指定空表的默认容量
    {
        this(16);
    }    public boolean isEmpty()                     //判断顺序表是否为空,若空返回true
    {                                            //O(1)
        return this.n==0;
    }    public int length()                          //返回顺序表长度,O(1)
    {
        return this.n;
    }    public E get(int index)                      //返回index位置的对象,index初值为0
    {                                            //O(1),若index指定序号无效则返回null
        if (index>=0 && index<this.n)
            return (E)this.table[index];         //使用了未经检查或不安全的操作
//            return table[index];               //可行,返回Object
        return null;
    }    public E set(int index, E element)           //设置index位置的对象为element,O(1)
    {                                            //若操作成功返回原对象,否则返回null
        if (index>=0 && index<this.n && element!=null)
        {
            E old = (E)this.table[index];
            this.table[index] = element;
            return old;
        }
        return null;
    }    public boolean add(int index, E element)     //在index位置插入element对象,O(n)
    {                                            //不能插入null,若操作成功返回true
        if (element==null)
           return false;                         //不能添加null
    
        if (this.n==table.length)                //若数组满,则需要扩充顺序表容量
        {
            Object[] temp = this.table;         
            this.table = new Object[temp.length*2];    //重新申请一个容量更大的数组
            for (int i=0; i<temp.length; i++)    //复制数组元素,O(n)
                this.table[i] = temp[i];
        }
             
        if (index<0)                             //下标容错
            index=0;
        if (index>this.n)
            index = this.n;        for (int j=this.n-1; j>=index; j--)      //元素后移,平均移动n/2
            this.table[j+1] = this.table[j];
        this.table[index] = element;
        this.n++;
        return true;
    }    public boolean add(E element)                //在顺序表最后插入element对象
    {
        return add(this.n, element);
    }
    
    public E remove(int index)                   //移去index位置的对象,O(n)
    {                                            //若操作成功返回被移去对象,否则返回null
        if (this.n!=0 && index>=0 && index<this.n) 
        {
            E old = (E)this.table[index];
            for (int j=index; j<this.n-1; j++)   //元素前移,平均移动n/2
                this.table[j] = this.table[j+1];
            this.table[this.n-1]=null;
            this.n--;
            return old;                          //若操作成功返回被移去对象
        }
        return null;                             //未找到删除对象,操作不成功
    }
/*
    public void clear()                          //清空线性表
    {
        if (this.n!=0) 
        {
            for (int i=0; i<this.n; i++)
                this.table[i] = null;
            this.n=0;
        }
    }
*/
    public void clear()                          //清空线性表
    {
        this.n=0;
    }    public String toString()                     //返回显示线性表所有元素值的字符串,形式为{,}
    {
        String str="(";
        if (this.n!=0)
        {
            for (int i=0; i<this.n-1; i++)
                str += this.table[i].toString()+", ";
            str += this.table[this.n-1].toString();
        }
        return str+") ";
    }
//以上实现LList接口,第2章内容
/*
可行,效率同上
    public String toString()                     //返回显示线性表所有元素值的字符串,形式为[,] 
    {
        String str="(";
        if (this.n()!=0)
        {
            for(int i=0; i<this.n()-1; i++)
                str += this.get(i).toString()+", ";
            str += this.get(this.n()-1).toString();
        }
        return str+")";
    }
*/

解决方案 »

  1.   

    这个问题,用最简单的数组就能解决!应该也会死效率最高的!
    public class SequentialTable {

    static int i=0,j=0,k=0;//表示a b c 三个数据的下标

     static int[] a={1,23,24,50,100};
     
     static int[] b={10,25,26,56,200};
     
     
     static  int[] c=new int[a.length+b.length];
     
     static void compare(){
     
     for(;i<a.length;){
     
        for(;j<b.length;){
        
         if (a[i]<=b[j]){
                      
                c[k]=a[i];
                 k++;
                 i++;
                 
                    // 如果i已经等于a.length,则说明a中的所有元素都赋值给c了,那么直接将b中元素追加到c的末尾
                  if(i==a.length){
                   for(; j<b.length;j++){
                   
                   c[k]=b[j];
                   
                   }
                   
                   
                  }
         }
            else {
        
                c[k]=b[j];
                 k++;
                 j++;
                 // 如果j已经等于b.length,则说明b中的所有元素都赋值给c了,那么直接将a中元素追加到c的末尾
                  if(j==b.length){
                   for(; i<a.length;i++){
                   
                   c[k]=a[i];
                   
                   }
        
         }
        
        }
     }
     
     }
     
    }
      public static void main(String[] args) {
         
    SequentialTable.compare();

    for(int y =0;y<c.length;y++){

    System.out.println(c[y]);

    } }}
      

  2.   

    要求用到泛型,如果顺序表存储的的对象不为int值,而是object型,怎么实现对象的排序。