如何用 Java 实现 有序,无序线性表 的合并 倒置

Java 实现线性表的合并倒置


有序表的合并

  • 两个有序数组的合并
public class Main {
    public static void main(String[] args) {
        int[] arr2 = {1,3,5,7};
        int[] arr1 = {4, 6, 8, 10, 12};
        ArraySort sort = new ArraySort();
        sort.sort(arr1, arr2);
    }
}

class ArraySort {
    public void sort(int[] arr1, int[] arr2) {
        int[] arr3 = new int[arr1.length + arr2.length];
        int i = 0, j = 0, k = 0;
        boolean flag = true;
        while (flag) {
            if (arr1[i] < arr2[j]) {
                arr3[k] = arr1[i];
                    i += 1;
            } else if (arr1[i] > arr2[j]) {
                arr3[k] = arr2[j];
                    j += 1;
            }
            k += 1;
            if(i == arr1.length){
                // arr1 遍历结束
                for (int s = j; s < arr2.length; s++) {
                    arr3[k] = arr2[s];
                    k+=1;
                }
                flag = false;
            }else if(j==arr2.length){
                // arr2 遍历结束
                for (int s = i; s < arr1.length; s++) {
                    arr3[k] = arr1[s];
                    k+=1;
                }
                flag = false;
            }
        }
        this.show(arr3);
    }
    public void show(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}

无序表的合并

public class Main {
    public static void main(String[] args) {
        int[] arr1 = {1, 3, 5, 7, 9};
        int[] arr2 = {2, 4, 6, 8, 10};
        ArraySort sort = new ArraySort();
        sort.sort(arr1, arr2);
    }
}

class ArraySort {
    public void sort(int[] arr1, int[] arr2) {
        int len = arr1.length;
        int[] arr3 = new int[len + arr2.length];
        for (int i = 0; i < len; i++) {
            arr3[i] = arr1[i];
        }
        for (int i = 0; i < arr2.length; i++) {

            if (!isExist(arr3, arr2[i])) {
                arr3[len + i] = arr2[i];
            }
        }
        show(arr3);
    }

    public boolean isExist(int[] arr, int b) {
        boolean reslt = false;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == b) {
                reslt = true;
            }
        }
        return reslt;
    }

    public void show(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
    }
}

线性表的倒置

  • 推荐第一种写法,不会产生垃圾
public class Hello {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        // 确定交换次数
        int center = arr.length / 2;
        int head = 0;
        int tail = arr.length - 1;
        for (int i = 0; i < center; i++) {
            int temp = arr[head];
            arr[head] = arr[tail];
            arr[tail] = temp;
            head++;
            tail--;
        }
        System.out.println(Arrays.toString(arr));
    }
}

public class Hello {
    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
        int j = 0;
        int[] temp = new int[arr.length];
        for (int i = arr.length - 1; i > -1; i--) {
            temp[j] = arr[i];
            j += 1;
        }
        arr = temp;
        System.out.println(Arrays.toString(arr));
    }
}

在这里插入图片描述

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页