4.2单向链表复习

import java.util.Iterator;

public class LinkList<T> implements Iterable<T> {
    //记录头结点
    private Node head;
    //记录链表的长度
    private int N;

    public LinkList() {
        head = new Node(null, null);
        N = 0;
    }

    // 清空链表
    public void clear() {
        head.next = null;
        head.item = null;
        N = 0;
    }

    // 获取链表的长度
    public int length() {
        return N;
    }

    //判断链表是否为空
    public boolean isEmpty() {
        return N == 0;
    }

    //获取指定位置i出的元素
    public T get(int i) {
        if (i < 0 || i >= N) {
            throw new RuntimeException("位置不合法!");
        }
        Node n = head.next;
        for (int index = 0; index < i; index++) {
            n = n.next;
        }
        return (T) n.item;
    }

    //向链表中添加元素t
    public void add(T t) {
        Node node = new Node(t, null);
        Node n = head;
        while (n.next != null) {
            n = n.next;
        }
        n.next = node;
        N++;
    }

    //向指定位置i处,添加元素t
    public void add(int i, T t) {
        if (i < 0 || i >= N) {
            throw new RuntimeException("位置不合法!");
        }
        Node pre = head;
        // pre 为 i-1 位置的结点
        for (int index = 0; index < i; index++) {
            pre = pre.next;
        }
        Node node = new Node(t, pre.next);
        pre.next = node;
        N++;
    }

    //删除指定位置i处的元素,并返回被删除的元素
    public T remove(int i) {
        if (i < 0 || i >= N) {
            throw new RuntimeException("位置不合法");
        }
        Node pre = head;
        for (int index = 0; index < i; index++) {
            pre = pre.next;
        }
        Node n = pre.next;
        pre.next = pre.next.next;
        N--;
        return (T) n.item;
    }

    //查找元素t在链表中第一次出现的位置
    public int indexOf(T t) {
        Node n = head;
        for (int index = 0; n.next != null; index++) {
            n = n.next;
            if (n.item.equals(t)) {
                return index;
            }
        }
        return -1;
    }

    // 链表反转
    public void reverse(){
        if(N==0)  return;
        reverse(head.next);
    }
    public Node reverse(Node curr) {
        //先递归到了最后一个元素
        if (curr.next == null) {
            head.next = curr;
            return curr;
        }
        // pre为curr的下个结点
        Node pre = reverse(curr.next);
        // 反转,将下个结点的next 指导 curr
        pre.next = curr;
        curr.next = null;
        return curr;
    }


    public void sout(Node pre) {
        if (pre != null) {
            while (pre.next != null) {
                pre = pre.next;
                System.out.printf(pre.item + " ");
            }
        } else {
            Node n = head;
            while (n.next != null) {
                n = n.next;
                System.out.println(n.item + " ");
            }
        }
    }


    //结点类
    private class Node {
        T item;
        Node next;

        public Node(T item, Node next) {
            this.item = item;
            this.next = next;
        }
    }

    @Override
    public Iterator<T> iterator() {
        return new Literator();
    }

    private class Literator implements Iterator {
        private Node n;

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

        @Override
        public boolean hasNext() {
            return n.next != null;
        }

        @Override
        public T next() {
            n = n.next;
            return (T) n.item;
        }
    }

    public static void main(String[] args) {
        LinkList<String> list = new LinkList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        //测试length方法
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println(list.length());
        System.out.println("-------------------");
        //测试get方法
        System.out.println(list.get(2));
        System.out.println("------------------------");
        //测试remove方法
        String remove = list.remove(1);
        System.out.println(remove);
        System.out.println(list.length());
        System.out.println("----------------");
        for (String s : list) {
            System.out.println(s);
        }
        list.clear();
        list.reverse();
        list.sout(null);
    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值