Java数据结构--链表

Java数据结构–链表

链表是一种基础的数据结构,相对于数组,他的增加和删除的时间复杂度减低

在这里插入图片描述

  • 链表类
import java.util.Iterator;

public class LinkList<T> implements Iterable<T> {
    private Node head;
    private int length;

    private class Node<T> {
        T item;
        Node next;

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

    LinkList() {
        this.head = new Node(null, null);
        length = 0;
    }

    /**
     * 清空链表
     */
    public void clear() {
        this.head.next = null;
        length = 0;
    }

    /**
     * 判断链表是否为空
     *
     * @return true链表为空 false链表不为空
     */
    public boolean isEmpty() {
        return length == 0;
    }

    /**
     * 求链表的长度
     *
     * @return 链表的长度
     */
    public int length() {
        return length;
    }

    /**
     * 检验i是否载合法的范围
     *
     * @param i 插入的位置
     * @return true合理
     * @Exception IndexOutOfBoundsException i的位置不合理
     */
    private boolean checkIndex(int i) {
        if (i > length - 1) {
            throw new IndexOutOfBoundsException("越界!!");
        } else {
            return true;
        }
    }

    /**
     * 获得位置i处的值
     *
     * @param i 插入的位置
     * @return 位置i处的值
     * @Exception IndexOutOfBoundsException i的位置不合理
     */
    public T get(int i) {
        Node temp = head.next;
        T value = null;
        if (checkIndex(i)) {
            for (int index = 0; index < i; index++) {
                temp = temp.next;
            }
            value = (T) temp.item;
        }
        return value;
    }

    /**
     * 插入元素t
     *
     * @param t 插入的元素的值(尾插法)
     */
    public void insert(T t) {
        Node temp = head;
        while (temp.next != null) {
            temp = temp.next;
        }
        Node newNode = new Node(t, null);
        temp.next = newNode;
        length++;
    }

    /**
     * 向指定位置插入值
     *
     * @param i 待插入的位置
     * @param t 插入的值
     * @Exception IndexOutOfBoundsException i的位置不合理
     */
    public void insert(int i, T t) {
        if (checkIndex(i)) {
            Node temp = head;
            for (int index = 0; index < i; index++) {
                temp = temp.next;
            }
            Node nextNode = temp.next;
            Node newNode = new Node(t, null);
            temp.next = newNode;
            newNode.next = nextNode;
            length++;
        }
    }

    /**
     * 删除指定位置的值
     *
     * @param i 待删除的位置
     * @return 删除的值
     * @Exception IndexOutOfBoundsException i的位置不合理
     */
    public T remove(int i) {
        T oldValue = null;
        if (checkIndex(i)) {
            Node temp = head;
            for (int index = 0; index < i; index++) {
                temp = temp.next;
            }
            oldValue = (T) temp.next.item;
            Node nextNode = temp.next.next;
            temp.next = nextNode;
            length--;
        }
        return oldValue;
    }

    /**
     * 返回t第一次出现的位置
     *
     * @param t 待查找的元素
     * @return -1没有该元素
     * t第一次出现的位置
     */
    public int indexOf(T t) {
        Node temp = head.next;
        for (int i = 0; i < length; i++) {
            if (temp.item.equals(t)) {
                return i;
            }
            temp = temp.next;
        }
        return -1;
    }

    /**
     * 用于增强for循环的遍历
     *
     * @return
     */
    @Override
    public Iterator<T> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator {
        private Node temp;

        public Itr() {
            this.temp = head;
        }

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

        @Override
        public Object next() {
            temp = temp.next;
            return temp.item;
        }
    }

    /**
     * 单链表反转
     */
    public void reverse() {
        if (isEmpty())
            return;
        else {
            reverse(head.next);
        }
    }

    /**
     * 针对某个节点的反转
     *
     * @param curr 待反转的节点
     * @return
     */
    public Node reverse(Node curr) {
        if (curr.next == null) {
            // System.out.println("  1   "+"此时curr的值:"+curr.item+"此时pre的值:");
            head.next = curr;
            return curr;
        }
        //System.out.println("   2    "+"此时curr的值:"+curr.item+"此时pre的值:");
        Node pre = reverse(curr.next);
        //System.out.println("    3    "+"此时curr的值:"+curr.item+"此时pre的值:"+pre.item);
        pre.next = curr;
        //System.out.println("    4    "+"此时curr的值:"+curr.item+"此时pre的值:"+pre.item);
        curr.next = null;
        //System.out.println("    5     "+"此时curr的值:"+curr.item+"此时pre的值:"+pre.item);
        return curr;
    }

    /**
     * 查找中间值
     *
     * @return 该链表中间处的值
     */
    public T FindMidden() {
        Node<T> frist = head;
        Node<T> second = head;
        while (second != null && second.next != null) {
            frist = frist.next;
            second = second.next.next;
        }
        return frist.item;
    }

    /**
     * 船舰一个环,有起始位置start和end连接
     * @param start 开始的连接地方
     * @param end 结束连接的地方
     * @Exception IndexOutOfBoundsException
     */
    public void CreateCircle(int start,int end){
        if(checkIndex(start)&&checkIndex(end)){

        }
    }
    /**
     * 判断链表是否有环
     *
     * @return true有环 false 无环
     */
    public boolean HasCircle() {
        Node<T> fast = head;
        Node<T> slow = head;
        while (fast != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow.equals(fast)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 寻找环的入口
     *
     * @param linkList 链表名
     * @return 环入口节点 null无环
     */
    public Node getEntance(LinkList linkList) {
        Node<T> fast = head;
        Node<T> slow = head;
        Node<T> temp = null;
        while (fast != null && fast.next.next != null) {
            slow = slow.next;
            fast = fast.next.next;
            if (slow.equals(fast)) {
                temp = slow;
                continue;
            }
            if (temp != null) {
                //判断临时指针是否和慢指针相遇
                temp = temp.next;
                if (temp.equals(slow)) {
                    break;
                }
            }
        }
        return temp;
    }
}
  • 测试类
public class LinkListTest
{
    public static void main(String[] args) {
        LinkList<String> linkList=new LinkList<>();
        linkList.insert("111");
        linkList.insert("222");
        linkList.insert("333");
        linkList.insert("444");
        linkList.insert(1,"ooo");
        //System.out.println(linkList.isEmpty());
        //System.out.println(linkList.get(2));
        //System.out.println(linkList.length());
        //System.out.println(linkList.remove(4));
        //linkList.reverse();
        for(String s:linkList)
        {
            System.out.println(s);
        }
        System.out.println(linkList.FindMidden());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值