合并单链表

来源:互联网 发布:河北网站seo 编辑:程序博客网 时间:2024/05/29 08:56

线性表接口

public interface ILinarList<E>{
    boolean add(E item); //添加元素
    boolean add(int index, E item);//插入元素
    E remove(int index); //删除元素
    int indexOf(E item); //定位元素
    E get(int index); //取表元素
    int size(); //求线性表长度
    void clear();//清空线性表
    boolean isEmpty(); //判断线性表是否为空
}

单链表实现线性表

public class SLinkList<E> implements ILinarList<E> {
private Node<E> start;// 单链表的头引用
int size;// 单链表的长度

        //链表数据类以及构造方法
private static class Node<E> {
E item;
Node<E> next;


Node(E element, Node<E> next) {
this.item = element;
this.next = next;
}
}


// 初始化线性表
public SLinkList() {
start = null;
}
// 添加元素,将元素添加在单链表的末尾
public boolean add(E item) {
if (start == null) {
start = new Node<E>(item, null);
} else {
Node<E> current = start;
while (current.next != null) {
current = current.next;
}
current.next = new Node<E>(item, null);
}
size++;
return true;
}
// 在单链表的第index索引位置前插入一个数据元素
public boolean add(int index, E item) {
Node<E> current;
Node<E> previous;
if (index < 0 || index > size) {
return false;
}
Node<E> newnode = new Node<E>(item, null);
// 在空链表或第一个元素前插入第一个元素
if (index == 0) {
newnode.next = start;
start = newnode;
size++;
} else {
// 单链表的两个元素间插入一个元素
current = start;
previous = null;
int j = 0;
while (current != null && j < index) {
previous = current;
current = current.next;
j++;
}
if (j == index) {
previous.next = newnode;
newnode.next = current;
size++;
}
}
return true;
}
// 删除单链表中的索引位置为index的数据元素
public E remove(int index) {
E oldValue = null;
if (isEmpty() || index < 0 || index > size - 1) {
oldValue = null;
}
Node<E> current = start;
if (index == 0) {
oldValue = current.item;
start = current.next;
size--;
} else {
Node<E> previous = null;
int j = 1;
while (current.next != null && j <= index) {
previous = current;
current = current.next;
j++;
}
previous.next = current.next;
oldValue = current.item;
current = null;
size--;
}
return oldValue;
}
// 在单链表中查找数据元素item数据位置
public int indexOf(E item) {
int index = 0;
if (item == null) {
for (Node<E> x = start; x != null; x = x.next) {
if (x.item == null)
return index;
index++;
}
} else {
for (Node<E> x = start; x != null; x = x.next) {
if (item.equals(x.item))
return index;
index++;
}
}
return -1;
}
// 获得单链表的第index索引位置的数据元素
public E get(int index) {
E item = null;
if (isEmpty() || index < 0 || index > size - 1) {
item = null;
}
Node<E> current = start;
int j = 0;
while (current.next != null && j < index) {
current = current.next;
j++;
}
if (j == index) {
item = current.item;
}
return item;
}
//求单链表长度
public int size() {
return size;
}
//清空单链表
public void clear() {
for (Node<E> x = start; x != null;) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x = next;
}
start = null;
size = 0;
}
//判断单链表是否为空
public boolean isEmpty() {
return size == 0;
}

//输出单链表
public void P(){
Node<E> current = start;
while(current != null){
System.out.print(current.item + " ");
 current=current.next;
}
System.out.println();

}

}



测试类

public class Test1 {
public static void main(String[] args) {
Scanner read=new Scanner(System.in);
while(read.hasNext()){
int n=read.nextInt();int m=read.nextInt();
int[] a=getRandomArrayByIndex(n,100);
int[] b=getRandomArrayByIndex(m,100);
SLinkList<Integer> A = new SLinkList<Integer>();
SLinkList<Integer> B = new SLinkList<Integer>();
System.out.println("排序前:");
System.out.println(Arrays.toString(a));
   System.out.println(Arrays.toString(b));
System.out.println("排序后:");
Arrays.sort(a);Arrays.sort(b);
   System.out.println(Arrays.toString(a));
   System.out.println(Arrays.toString(b));
   for(int i=0;i<n;i++){
          A.add(a[i]); 
   }
   for(int i=0;i<m;i++){
      B.add(b[i]); 
}
   System.out.println("合并后:");
   merge(A,B);
   A.P();

}
  read.close();
}

//参数说明:num--无重复随机数的个数   scope--随机数所在范围(不包含scope)
    public static int[] getRandomArrayByIndex(int num,int scope){
        //1.获取scope范围内的所有数值,并存到数组中
        int[] randomArray=new int[scope];
        for(int i=0;i<randomArray.length;i++){
            randomArray[i]=i;
        }


        //2.从数组random中取数据,取过后的数改为-1 
        int[] numArray=new int[num];//存储num个随机数
        int i=0;
        while(i<numArray.length){
            int index=(int)(Math.random()*scope+1);
            if(randomArray[index]!=-1){
                numArray[i]=randomArray[index];
                randomArray[index]=-1;
                i++;
            }
        }
        return numArray;
    }


    //合并单链表
  public static void merge(SLinkList<Integer> A,SLinkList<Integer> B) {
  int o=0,p=0,s=0;
        while(s<B.size()){
        p=s;
        while(p<B.size()){
        if(A.get(o)>B.get(p)){
    A.add(o,B.get(p));s++;  
                } 
        p++;
        }
        o++;   
  }
  }
 
}