java-带头结点的链表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cheng_cuo_tuo/article/details/80713095

程序小白,希望和大家多交流,共同学习

//带头结点的单向链表
import java.util.Scanner;

public class MyLinkedList{
    private static class MyNode{
        private int data;
        private MyNode next;

        public MyNode(){
        }
        public MyNode(int data){
            this.data = data;
        }
    }

    private MyNode head;
    private Scanner input = new Scanner(System.in);
    public MyLinkedList(){
        head = new MyNode();
        head.next = null;
    }

    public MyNode getHead(){
        return this.head;
    }

    public void createLinkedListFromHead(){
        int num = 0;
        MyNode node = null;
        System.out.println("头插法,创建链表,输入数据:");
        while (true){
            num = input.nextInt();
            if (num == -1){
                break;
            }
            else{
                node = new MyNode(num);
                node.next = head.next;
                head.next = node;
            }
        }
    }

    public void createLinkedListFromTail(){
        int num = 0;
        MyNode node = null;
        MyNode tail = head;
        System.out.println("尾插法,创建链表,输入数据:");
        while (true){
            num = input.nextInt();
            if (num == -1){
                tail.next = null;
                break;
            }
            else{
                node = new MyNode(num);
                tail.next = node;
                tail = node;
                //System.out.println(tail.data);
            }
        }
    }
    public void deleteNode(int index){
        if (index < 0){
            System.out.println("输入位置过小");
            return ;
        }
        MyNode preNode = head;
        int i = 0;
        while (i < index - 1 && preNode.next != null){
            preNode = preNode.next;
            i++;
        }
        if (preNode.next == null){
            System.out.println("输入位置过大");
            return;
        }

        MyNode nextNode = preNode.next;
        preNode.next = nextNode.next;
    }
    @Override
    public String toString(){
        StringBuilder info = new StringBuilder("[ ");
        MyNode node = head.next;
        while (node != null){
            info.append(node.data + " ");
            node = node.next;
        }
        info.append("]");
        return info.toString();
    }

    public int getLength(){
        int len = 0;
        MyNode node = head;
        while (node.next != null){
            len++;
            node = node.next;
        }
        return len;
    }

    public MyNode get_UseData(int data){
        MyNode node = head.next;
        while (node != null){
            if (node.data == data){
                return node;
            }
            node = node.next;
        }
        System.out.println("不在链表内");
        return null;
    }

    public MyNode get_UseIndex(int index){
        if (index < 0){
            System.out.println("按下标查找,输入位置过小");
            return null;
        }

        MyNode node = head;
        int i = 0;
        while (i < index && node.next != null){
            node = node.next;
            i++;
        }
        if (i != index){
            System.out.println("按下标查找,输入位置过大");
            return null;
        }
        else
            return node;
    }

    public void insert(int index, int data){
        if (index < 0){
            System.out.println("插入,输入位置过小");
            return ;
        }

        MyNode node = null;
        if (index == 0){ 
            node = new MyNode(data);
            node.next = head.next;
            head.next = node;
        }
        else{
            node = get_UseIndex(index - 1);
            MyNode newNode = new MyNode(data);
            newNode.next = node.next;
            node.next = newNode;
        }
    }

    public static void merge(MyLinkedList l1, MyLinkedList l2, MyLinkedList l3){
        MyNode node1 = l1.head.next;
        MyNode node2 = l2.head.next;

        l3.head = l1.head;
        l3.head.next = null;
        MyNode tail = l3.head;

        while (node1 != null && node2 != null){
            //System.out.println(node1.data + " " + node2.data + " " + tail.data);
            if (node1.data < node2.data){
                tail.next = node1;
                tail = node1;
                node1 = node1.next;
                //System.out.println("merge node1");
            }
            else{
                tail.next = node2;
                tail = node2;
                node2 = node2.next;
                //System.out.println("merge node2");
            }
        }
        if (node1 != null){
            tail.next = node1;
        }
        if (node2 != null){
            tail.next = node2;
        }
    }

    public void invert(){//手动模拟一次就清楚了
        MyNode node = head.next;
        head.next = null;
        MyNode nextNode = null;
        while (node != null){
            nextNode = node.next;
            node.next = head.next;
            head.next = node;

            node = nextNode;
        }
    }

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        MyLinkedList l1 = new MyLinkedList();
        l1.createLinkedListFromHead();
        System.out.println(l1.toString() + " " + l1.getLength());

        MyLinkedList l2 = new MyLinkedList();
        l2.createLinkedListFromTail();
        System.out.println(l2.toString() + " " + l2.getLength());

        System.out.println("在第二个链表中,按值查找: ");
        int num = in.nextInt();
        System.out.println(l2.get_UseData(num).data);

        System.out.println("在第二个链表中,按下标查找: ");
        int index1 = in.nextInt();
        System.out.println(l2.get_UseIndex(index1).data);

        System.out.println("删除结点,输入下标:");
        int index2 = in.nextInt();
        l2.deleteNode(index2);
        System.out.println(l2.toString());

        System.out.println("插入结点,输入下标,数据:");
        int index3 = in.nextInt();
        int num2 = in.nextInt();
        l2.insert(index3, num2);
        System.out.println(l2.toString());

        MyLinkedList l3 = new MyLinkedList();
        l3.createLinkedListFromTail();
        System.out.println(l3.toString());
        System.out.println("合并两个链表:");
        MyLinkedList l4 = new MyLinkedList();
        MyLinkedList.merge(l2, l3, l4);
        System.out.println(l4.toString());

        l2.invert();
        System.out.println(l2.toString());
    }
}
展开阅读全文

没有更多推荐了,返回首页