Java实现单向链表,实现增加,删除,遍历三种方法

package day12;

public class LinkedList {
          
   
    public static void main(String[] args) {
          
   
    //测试功能
        LinkedList list = new LinkedList();
        list.insert(0, 1);
        list.add("asd");
        list.add(123);
        list.add("111111");
        list.add("55555555555555");
        list.insert(3, 12);
        list.remove("asd");
        list.find(123);

        list.print();
    }

    public static class ListNode {
          
   
        Object date;
        ListNode next;

        public ListNode(Object date) {
          
   
            this.date = date;

        }
    }

    ListNode head;
    ListNode end;
    int size;

    public LinkedList() {
          
   
        head = null;//头节点
        end = null;//尾节点
        size = 0;//链表中节点个数
    }


    //在末尾加入一个节点
    public void add(Object date) {
          
   
        ListNode newNode = new ListNode(date);
        if (end == null) {
          
   
            end = newNode;
        } else {
          
   
            end.next = newNode;
            end = newNode;
        }
        size++;
    }

    public void insert(int position, Object date) {
          
   
        if (position > size) {
          
   
            return;
        }
        ListNode newNode = new ListNode(date);
        //当插入的位置是头节点时
        if (position == 0) {
          
   
            newNode.next = head;
            head = newNode;
            //当插入的位置是头节点且尾节点为null(也就是链表是空的的时候)
            if (end == null) {
          
   
                end = newNode;
            }
            size++;
        } else if (position == size) {
          
   
            //如果是在末尾插入
            this.add(date);
        } else {
          
   

            /**如果是在中间位置插入
             * 找到目标位置的前一个节点
             */

            ListNode a = head;
            for (int i = 0; i < position - 1; i++) {
          
   
                if (a.next == null) {
          
   
                    break;
                }
                a = a.next;
            }
            ListNode b = a.next;
            newNode.next = b;
            a.next = newNode;
            size++;
        }
    }

    public void remove(Object date) {
          
   
        //如果要删除的是头节点
        if (head != null && head.date == date) {
          
   
            head = head.next;
            size--;
            if (size == 0) {
          
   
                head = end;
            }
        } else {
          
   
            ListNode a = head;
            ListNode b = a.next;
            while (b != null) {
          
   
                if (b.date == date) {
          
   
                    //如果要删除的是尾节点
                    if (b == end) {
          
   
                        end = a;
                    } else {
          
   
                        a.next = b.next;
                        size--;
                        return;
                    }
                }
            }
        }
    }

    public void find(Object date) {
          
   
        ListNode a = head;
        for (int i = 0; a != null; i++) {
          
   
            if (a.date == date) {
          
   
                System.out.println("第" + i + "个节点");
                break;
            }
            a = a.next;
        }


    }

    public void print() {
          
   
        ListNode a = head;
        while (a != null) {
          
   
            System.out.print(a.date + ",");
            a = a.next;
        }
    }
}
经验分享 程序员 微信小程序 职场和发展