Java实现单链表

java实现单链表

在这里插入图片描述
单链表:
add(T v)
add(int index,Tv)
get(int index)
remove(int index)
size()

public class LinkList<T> implements Iterable<T>{

    //节点
    private class Node<U>{
        public T item;
        //下一个节点
        public Node<U> next;

        public Node(T item,Node<U> next){
            this.item = item;
            this.next = next;
        }
    }
    //链表长度
    private int N;
    //记录头节点
    private Node<T> head;

    public LinkList() {
        //初始化第一个节点
        head = new Node<>(null, null);
        N = 0;
    }

    /**
     * 删除指定位置
     */
    public void remove(int index) {
        if(index < 0 || index > N){
            throw new RuntimeException("索引超限");
        }
        //获取当前节点
        Node<T> current = getNode(index);
        if(index == N-1){
            //最后一个节点
            head = current.next;
        }else{
            //获取原来节点的上一个节点
            Node<T> nextNode = getNode(index + 1);
            //原来节点上一个节点的下一个节点现在是原来节点的下一个节点
            nextNode.next = current.next;
        }
        N--;

    }

    /**
     * 在指定位置添加数据
     */
    public void add(int index, T value) {
        if(index < 0 || index > N){
            throw new RuntimeException("索引超限");
        }
        //放在末尾
        if(index >= N) {
            add(value);
        }else{
            //index原有位置节点
            Node<T> current = getNode(index);
            //原有位置节点的下一个节点
            Node<T> pre = current.next;
            //new一个新的节点
            Node<T> newNode = new Node<>(value, null);
            current.next = newNode;
            //如果下一个节点不为空,那么就可以插入
            if(pre != null){
                newNode.next = pre;
            }
        }
        N++;
    }

    /**
     * 添加操作
     */
    public void add(T t){
        //找到当前节点
        Node<T> n = head;
        //当前节点值为空,说明是第一节节点
        if(n.item == null){
            Node<T> newNode = new Node<>(t, null);
            newNode.next = n;
            head = newNode;
        }else{
            //当前节点
            Node<T> newNode = new Node<>(t, head);
            head = newNode;
        }
        //链表长度+1
        N++;
    }

    public Integer size(){
        return N;
    }

    /**
     * 根据指定索引查询
     */
    public T get(int index) {
        return getNode(index).item;
    }

    public Node<T> getNode(int index) {
        if(index < 0 || index >= N){
            throw new RuntimeException("索引超限");
        }
        Node<T> n = head;
        // 遍历搜素
       for(int i=0;i<N-index-1;i++){
           n = n.next;
       }
        return n;
    }

    public static void main(String[] args) {
        LinkList<String> list = new LinkList<>();
        list.add("1");
        list.add("2");
        list.add("2222");
        list.add("1");
        list.add("2");
        list.add("2222");
        list.remove(5);
        list.remove(0);
        list.remove(3);
//        System.out.println(list.get(0));
//        System.out.println(list.get(1));
//        System.out.println(list.get(2));
        Iterator it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    /**
     * 迭代器
     * @return
     */
    @Override
    public Iterator<T> iterator() {
        return new LIterator();
    }

    /**
     * 内部类实现一个迭代器
     */
    private class LIterator implements Iterator<T> {
        private Node<T> n;

        public LIterator() {
            this.n = head;
        }

        @Override
        public boolean hasNext() {
            Node<T> a = n;
            return a.next != null;
        }

        @Override
        public T next() {
            Node<T> a = n;
            n = n.next;
            return a.item;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值