编辑代码

import java.util.Scanner;
interface List {
    void createList();  // 线性表的建立
    void insert(int index, int value);  // 线性表的插入
    void delete(int index);  // 线性表的删除
    int search(int value);  // 线性表的查找
    void merge(List list);  // 线性表的合并
    void printList();
}

class ListNode {
    int val;
    ListNode next;
    ListNode(int val) {
        this.val = val;
        this.next = null;
    }
}

class LinkedList implements List {
    ListNode head = null, tail = null;
    @Override
    public void createList() {  // 链表的建立
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入线性表长度:");
        int n = scanner.nextInt();
        System.out.print("请依次输入线性表元素(用空格隔开):");
        for (int i = 0; i < n; i++) {
            int val = scanner.nextInt();
            ListNode node = new ListNode(val);
            if (tail == null) {
                head = tail = node;
            } else {
                tail.next = node;
                tail = node;
            }
        }
    }

@Override
    public void insert(int index, int value) { // 链表的插入
        if (index < 0) {
            System.out.println("插入位置不合法!");
            return;
        }
        ListNode node = new ListNode(value);
        if (index == 0) {
            node.next = head;
            head = node;
        } else {
            ListNode pre = head;
            for (int i = 0; i < index - 1; i++) {
                if (pre == null) {
                    System.out.println("插入位置不合法!");
                    return;
                }
                pre = pre.next;
            }
            if (pre == null) {
                System.out.println("插入位置不合法!");
                return;
            }
            node.next = pre.next;
            pre.next = node;
            if (pre == tail) {
                tail = node;
            }
        }
    }

    @Override
    public void delete(int index) { // 链表的删除
        if (index < 0 || head == null) {
            System.out.println("删除位置不合法!");
            return;
        }
        if (index == 0) {
            head = head.next;
            if (head == null) {
                tail = null;
            }
        } else {
            ListNode pre = head;
            for (int i = 0; i < index - 1; i++) {
                if (pre.next == null) {
                    System.out.println("删除位置不合法!");
                    return;
                }
                pre = pre.next;
            }
            if (pre.next == null) {
                System.out.println("删除位置不合法!");
                return;
            }
            pre.next = pre.next.next;
            if (pre.next == null) {
                tail = pre;
            }
        }
    }

    @Override
    public int search(int value) { // 链表的查找
        ListNode cur = head;
        int index = 0;
        while (cur != null) {
            if (cur.val == value) {
                return index;
            }
            cur = cur.next;
            index++;
        }
        return -1;
    }

    @Override
    public void merge(List list) { // 链表的合并
        if (!(list instanceof LinkedList)) {
            System.out.println("不支持的合并操作!");
            return;
        }
        LinkedList linkedList = (LinkedList) list;
        if (linkedList.head == null) {
            return;
        }
        if (head == null) {
            head = linkedList.head;
            tail = linkedList.tail;
            return;
        }
        tail.next = linkedList.head;
        tail = linkedList.tail;
    }

    @Override
    public void printList() { // 打印链表
        ListNode cur = head;
        System.out.print("当前链表为:");
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }