Custom linked list increase, delete, linked list reverse order

I have been working for a long time and I often use the framework. I feel that it has become a coding. It is recommended to flash the lettcode if you have time. It will be forgotten after a long time. After writing it for a long time, record it and write it after understanding. It is very easy to write it in reverse order. Blogs are written in various ways, but I feel that the explanation is still unclear, so I will summarize here:

1. First define the structure of the linked list 

The defined structure Node<T> is a generic type that data can be customized, which increases flexibility:

public class Node<T> {
    public T data;
    public Node next; //可以理解为C语言的指针
    public Node(){}
    public Node(T data){
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}

 The head pointer data designed here is empty by default, which is null;

 Execute temp =p ,p=q The execution process is as follows:

 

 

Execute tmp =p;p=q;q=q->next where q=q->next is C/C++-like writing for explanation; 

 It is easy to overlook here: p.next=null is easy to form a loop:

 q=q->next, here I understand it as a probe, whether the query has reached the end of the linked list: 

 Additions and deletions, reverse order, the specific code is as follows:

import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 * @ClassName MyList
 * @Description
 * @Author qianxl
 * @Date 2019-09-07 17:34
 * @Version 1.0
 **/
public class MyList<T> {
    public Node head = new Node(-1);
    public Node current;

    /**
     * @param data
     * @description: 添加元素
     * @return: {@link Node}
     * @author qianxl
     * @date: 2019/9/7
     * @since 1.0
     */
    public Node add(T data) {
        Node temp = head;
        Node before = new Node();
        do {
            before = temp;
        } while ((temp = temp.next) != null);
//注释部分是代码重构使用do while
//        before = temp;
//        while (temp != null) {
//            before = temp;
//            temp = temp.next;
//        }
        temp = new Node(data);
        before.next = temp;
        return head;
    }

    public Node remove(T values) {
        Node data = this.head;
        boolean flag = false;
        Node before;
        Node temp = data.next;
        do {
            before = data;
            if (values.equals(temp.data)) {
                flag = true;
                break;
            }
        } while ((temp = temp.next) != null);
//注释部分是代码重构使用do while
//        Node temp = data.next;
//        Node before =data;
//        while (temp != null) {
//            if (values.equals(temp.data)) {
//                flag = true;
//                break;
//            }
//            before = temp;
//            temp = temp.next;
//        }
        if (flag) {
            before.next = temp.next;
        }
        return head;
    }

    /**
     * @param
     * @description: 计算集合的长度
     * @return: {@link int}
     * @author qianxl
     * @date: 2019/9/7
     * @since 1.0
     */
    public int size() {
        Node data = head;
        int count = 0;
        Node tmp;
        while (data != null) {
            data = data.next;
            count++;
        }
        return count - 1;//head 不存入数据
    }

    /**
     * @param index
     * @param value
     * @description: 指定位置插入值
     * @return: {@link Node}
     * @author qianxl
     * @date: 2019/9/8
     * @since 1.0
     */
    public Node insert(int index, T value) {
        Node data = head.next; //去掉头指针
        if (index > this.size() || index < 1) {
            return head;
        }
        if (index == 1) {
            Node node = new Node(value);
            node.next = head.next;   //将新建的节点的指针指向,之前head 头结点指向的指针
            head.next = node;
            return head;
        }
        int count = 1;
        Node before = data; //
        do {
            if (index == count) {
                Node node = new Node(value);
                node.next = data;
                before.next = node; //指向新建的节点
                break;
            }
            count++;
            before = data;
            data = data.next;
        } while (data != null);
//注释部分是代码重构使用do while
//        Node before =data;
//        while (data != null) {
//            if (index == count) {
//                Node  node = new Node(value);
//                node.next=data;
//                before.next=node; //指向新建的节点
//                break;
//            }
//           count++;
//            before = data;
//            data = data.next;
//        }

        return head;

    }

    /**
     * @param
     * @description: 链表逆序
     * @return: {@link Node}
     * @author qianxl
     * @date: 2019/9/8
     * @since 1.0
     */
    public Node reverse() {
        Node p,q=null;
        p = head.next; //指针 引用
        q = head.next.next;//q 指针可以理解为探针,在探是否到达链表末尾了

        Node tmp=null;
        p.setNext(null); //防止回环
        while (q != null) {
           tmp =p;
           p=q;
           q=q.next;   //q=q->next 起到探针的作用
           p.next=tmp;

        }
        head.next =p;
        return head;
    }

     public void print(Node node) {
        if (node.next == null) {
            return;
        }
        Node temp = node.next;
        while (temp != null) {
            System.out.print(temp.data+"  ");
            temp = temp.next;
        }
        System.out.println();
    }

    /**
     * @param array
     * @description: 将数组转换为list   head.next 理解为C语言指针,写链表操作一定要画图!
     * @return: {@link Node}
     * @author qianxl
     * @date: 2019/9/7
     * @since 1.0
     */
    public Node arrayToList(T[] array) {
        Node data = head;
        for (int i = 0; i < array.length; i++) {
            Node node = new Node(array[i]);
            data.next = node;  //表情指向前驱
            data = node;  //表示
        }
        return head;
    }


    public static void main(String arg[]) {
        MyList<Object> list = new MyList<>();
        Node ddd = list.add("ddd");

        list.add("this is a list");
        list.add("fffff");
        list.remove("ddd");
    
        list.print(ddd);
        Node node = list.arrayToList(new String[]{"2","3","4"});
        list.print(node);
        // list.insert(1, "8");
        list.add("8");
        list.print(node);
        System.out.println();
        list.reverse();
        list.print(node);
    }
}

to sum up

Guess you like

Origin blog.csdn.net/JHON07/article/details/100621275