今日分享 单向链表代码 (java版)

package com.itheima.demo1;

import java.util.Iterator;
import java.util.function.Consumer;

//单向链表
public class SinglyLinkedList implements Iterable<Integer> {//整体

    private Node head = null;//头指针

    /*
     *节点类implements Iterable<Integer>
     */
    private static class Node {//内部类使用外部类的成员不加static
        int value;//值
        Node next;//下一个节点指针

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }


        public Node() {
        }

        /**
         * 获取
         *
         * @return value
         */
        public int getValue() {
            return value;
        }

        /**
         * 设置
         *
         * @param value
         */
        public void setValue(int value) {
            this.value = value;
        }

        /**
         * 获取
         *
         * @return next
         */
        public Node getNext() {
            return next;
        }

        /**
         * 设置
         *
         * @param next
         */
        public void setNext(Node next) {
            this.next = next;
        }

        public String toString() {
            return "Node{value = " + value + ", next = " + next + "}";
        }
    }

    //插入(向链表头部添加)
    public void addFirst(int value) {
        //1.链表为空;
        //head=new Node(value,null);
        //2.链表非空
        head = new Node(value, head);
    }

//    //插入(向链表尾部添加)
//    public void addLast(int value) {
//        Node last = head;
//        if (last == null) {
//            addFirst(value);
//            return;
//        }
//        while(last.next!=null){
//            last=last.next;
//        }
//
//        last.next = new Node(value, last.next);
//    }

    //插入(向链表尾部添加)
    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, last.next);
    }

    //找最后一个节点
    private Node findLast() {
        if (head == null) {
            return null;
        }
        Node p = head;
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }

//    public void test() {
//        int i = 0;
//        Node p = head;
//        while (p != null) {
//            System.out.println(i + ":" + p.value);
//            i++;
//            p = p.next;
//        }
//    }

    //给定索引返回节点
    private Node findNode(int index) {
        int i = 0;
        Node p = head;
        while (p != null) {
            if (index == i) {
                return p;
            }
            i++;
            p = p.next;
        }
        return null;
    }

    public int get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new IllegalArgumentException(
                    String.format("index[%d]不合法%n", index));
        }
        return node.value;
    }

    //遍历1
    public void loop1(Consumer<Integer> consumer) {
        Node p = head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;

        }
    }

    //遍历2
    public void loop2(Consumer<Integer> consumer) {
        for (Node p = head; p != null; p = p.next) {
            consumer.accept(p.value);
        }
    }

    //递归遍历
    public void loop3(Consumer<Integer> before, Consumer<Integer> after){
        recursion(head,before,after);
    }

    private  void recursion(Node curr, Consumer<Integer> before, Consumer<Integer> after){
        if(curr==null){
            return;
        }
        before.accept(curr.value);
        recursion(curr.next, before, after);
        after.accept(curr.value);
    }

    //遍历3
    @Override
    public Iterator<Integer> iterator() {
        //匿名内部类
        return new Iterator<Integer>() {
            Node p = head;

            @Override
            public boolean hasNext() {//询问是否有下一个元素
                return p != null;//是真,否假
            }

            @Override
            public Integer next() {//返回当前元素值。,并指向下一个元素
                int v = p.value;
                p = p.next;
                return v;
            }

        };
    }

    //向索引位置插入
    public void insert(int index, int value) {
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node prev = findNode(index - 1);
        if (prev == null) {
            throw illegalIndex(index);
        }
        prev.next = new Node(value, prev.next);
    }

    private IllegalArgumentException illegalIndex(int index) {
        return new IllegalArgumentException(
                String.format("index[%d]不合法%n", index));
    }

    //删除第一个节点
    public void removeFirst() {
        if (head == null) {
            throw illegalIndex(0);
        }
        head = head.next;
    }

    //根据索引删除节点
    public void remove(int index) {
        if (index == 0) {
            removeFirst();
            return;
        }
        Node prev = findNode(index - 1);
        if (prev == null || prev.next == null) {
            throw illegalIndex(index);
        }
        prev.next = prev.next.next;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值