自定义链表增,删除,链表逆序

工作时间长了,经常用框架,感觉真的成了coding,建议有时间可以刷一下lettcode 时间一长就会忘,写了大半天,记录一下,理解后再写特别简单,链表逆序看了很多博客,写法各式各样,但是感觉解释的还是不清楚明了,所以在这又总结一下:

1.首先定义链表的结构 

定义的结构Node<T> 这样data 可以自定义的泛型,增加了灵活性:

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;
    }
}

 这里设计的 head 头指针数据默认为空也就是null;

 执行 temp =p ,p=q 执行流程如下:

执行 tmp =p;p=q;q=q->next 其中q=q->next 是类C/C++写法进行解释; 

 这里容易忽视:p.next=null 容易形成回环:

扫描二维码关注公众号,回复: 12310788 查看本文章

 q=q->next,在这里我理解为探针,查询是否已经到链表结尾了: 

 增删,逆序,具体代码如下:

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);
    }
}

总结

猜你喜欢

转载自blog.csdn.net/JHON07/article/details/100621275