Java-容器-自定义实现LinkedList

本文介绍了如何使用Java实现一个名为MyLinkedList的自定义线性数据结构,包括添加、设置、获取元素、删除以及遍历等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package collection;

public class MyLinkedList<E> implements MyList {
    public static void main(String[] args) {
        MyLinkedList<String> list = new MyLinkedList<>();
        list.add("aa");
        list.add("bb");
        list.add("cc");
//        Node n = list.node(2);
//        System.out.println(n);
        list.add(1,"dd");
        Node n2 = list.node(1);
//        System.out.println(n2.obj);
//        list.remove(1);
//        list.remove("bb");
//        list.clear();
        list.set(1,"judy");
        String str = list.get(1);
        System.out.println(str);
        System.out.println(list.toString());
    }
    private int size;
    private Node first;//第一个节点
    private Node last;//最后一个节点

    //    索引是否合法
    private void rangeCheck(int index){
        if (index<0 || index>=size){
            try {
                throw new Exception("索引必须位于0到size-1之间");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
//  根据index找到对应的node节点
    private Node node(int index){
        rangeCheck(index);
        Node temp = null;
        if (first!=null){
            if (index<=size/2){
                temp = first;
                for (int i = 0; i < index; i++) {
                    temp = temp.next;
                }
            }else {
                temp = last;
                for (int i = size-1; i < index; i--) {
                    temp = temp.previous;
                }
            }
        }
        return temp;
    }
    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size==0;
    }
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(node(i).obj+"\t");
        }
        sb.append("]");
        return sb.toString();
    }

    @Override
    public void add(Object obj) {
//        给容器中增加一个obj,对应实际上需要增加一个节点(用这个节点来存储obj)
        Node n = new Node();
        if (first==null){
            n.setPrevious(null);
            n.setObj(obj);
            n.setNext(null);

//            由于只有一个节点,first也指向n,last也指向n
            first = n;
            last = n;
        }else{
            n.setPrevious(last);
            n.setObj(obj);
            n.setNext(null);

            last.setNext(n);
            last = n;
        }
        size++;
    }

    @Override
    public void add(int index, Object obj) {
        Node oldNode = node(index);//要插入的位置的节点(旧节点)
        Node newNode = new Node();//创建一个新节点
        newNode.obj = obj;//给新节点赋值
        if (oldNode!=null){
            Node up = oldNode.previous;
            up.next = newNode;
            newNode.previous = up;

            newNode.next = oldNode;
            oldNode.previous = newNode;
        }
        size++;
    }

    @Override
    public E set(int index, Object obj) {
        Node temp = node(index);
        temp.obj = obj;

        return (E)obj;
    }

    @Override
    public E get(int index) {

        return (E)node(index).obj;
    }

    @Override
    public boolean contains(Object obj) {
        for (int i = 0; i < size; i++) {
            if(node(i).obj.equals(obj)){
                return true;
            }
        }
        return false;
    }

    @Override
    public Object[] toArray() {
        Object[] arr = new Object[size];
        for (int i = 0; i < size; i++) {
            arr[i] = node(i).obj;
        }
        return arr;
    }

    @Override
    public boolean remove(Object obj) {
        for (int i = 0; i < size; i++) {
            if (node(i).obj.equals(obj)){
                remove(i);
                return true;
            }
        }
        return false;
    }

    @Override
    public void remove(int index) {
        Node temp = node(index);//找到要移除的节点
        if (temp!=null){
            Node up = temp.previous;//上一个节点
            Node down = temp.next;//下一个节点
            up.next = down;
            down.previous = up;
            size--;

        }
    }

    @Override
    public void clear() {
        for (int i = 0; i < size; i++) {
            remove(i);
        }
    }
}
/**
 * 链表中的节点类
 */
class Node {
    Node previous;//上一个节点
    Node next;//下一个节点
    Object obj;//真正存储的信息

    public Node getPrevious() {
        return previous;
    }

    public void setPrevious(Node previous) {
        this.previous = previous;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Object getObj() {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj;
    }

    public Node(Node previous, Node next, Object obj) {
        this.previous = previous;
        this.next = next;
        this.obj = obj;
    }
    public Node(){}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值