菜单

duckflew
发布于 2020-10-15 / 166 阅读
0
0

链表

链表

public class LinkListPractice<E>
{
    private class Node
    {
        E data;
        Node next;

        public Node(E data, Node next)
        {
            this.data = data;
            this.next = next;
        }

        @Override
        public String toString()
        {
            return data.toString();
        }
    }
    private Node head;
    private int size;

    public LinkListPractice()
    {
        head= new Node(null,null);
        size=0;
    }
    public int length(){return size;}
    public boolean isEmpty(){return size==0;}

    /**
     * 向指定位置添加元素
     * @param index
     * @param data
     */
    public void add(int index,E data)
    {
        if (index<0||index>size)throw new IllegalArgumentException("illegal index!");
        Node pre=head;
        for (int i=0;i<size;i++)
        {
            pre=pre.next;
        }
        pre.next=new Node(data,pre.next);
        size++;
    }
    public void addFirst(E data)
    {
        add(0,data);
    }
    public void addLast(E data)
    {
        add(size,data);
    }

    @Override
    public String toString()
    {
        StringBuilder builder=new StringBuilder();
        builder.append("length:"+length()+"\n");
        builder.append("head");
        Node cur=head.next;
        while (cur!=null)
        {
            builder.append("->"+cur.data);
            cur=cur.next;
        }
        builder.append("->null");
        return builder.toString();
    }

    /**
     * 获取指定位置的元素
     * @param index
     * @return
     */
    public E get(int index)
    {
        if (index<0||index>=size)throw new IllegalArgumentException("illegal index!");
        Node cur =head.next;
        for (int i=0;i<size;i++)
        {
            cur=cur.next;
        }
        return cur.data;
    }

    public E getFirst()
    {
        return get(0);
    }
    public E getLast()
    {
        return get(size-1);
    }

    /**
     * 删除指定位置的元素
     * @param index
     * @return
     */
    public E remove(int index)
    {
        if (index<0||index>=size)throw new IllegalArgumentException("illegal index!");
        Node pre =head;
        for (int i=0;i<index;i++)
        {
            pre=pre.next;
        }
        Node toDeleteNode=pre.next;
        pre.next=toDeleteNode.next;
        toDeleteNode.next=null;
        size--;
        return toDeleteNode.data;
    }

    /**
     * 删除第一个出现的data元素
     * @param data
     */
    public void removeData(E data)
    {
        Node pre =head;
        while (pre.next!=null)
        {
            if (pre.next.data.equals(data))
            {
                Node toDeleteNode = pre.next;
                pre.next = toDeleteNode.next;
                size--;
                toDeleteNode.next = null;
                break;
            }
            else
            {
                pre=pre.next;
            }
        }
    }
    public E removeFirst(){return remove(0);}
    public E removeLast(){return remove(size-1);}

    /**
     * 删除所有出现的data
     * @param data
     */
    public void removeAll(E data)
    {
        Node pre =head;
        while (pre.next!=null)
        {
            if (pre.next.data.equals(data))
            {
                Node toDeleteNode = pre.next;
                pre.next = toDeleteNode.next;
                size--;
                toDeleteNode.next = null;
            }
            else
            {
                pre=pre.next;
            }
        }
    }
    public boolean contains(E data)
    {
        Node cur =head.next;
        while (cur.next!=null)
        {
            if (cur.next.data.equals(data))
                return true;
                cur=cur.next;
        }
        return false;
    }
}


评论