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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值