JAVA实现LinkedList链表

本文介绍了一个简单的自定义链表实现,包括基本操作如添加、删除等,并通过具体代码展示了如何构建一个双向链表。

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

package com.github.Ven13.coding2017.basic;

//MyLinkedList功能类:
public class MyLinkedList extends MyAbstractList {

private Node first, last;

private static class Node {

    Object element;
    Node next;

    public Node(Object element) {
        this.element = element;
    }

}

public MyLinkedList(Object[] objects) {
    super(objects);
}

public MyLinkedList() {

}

public Object getFirst() {

    if (size == 0) {
        return null;
    } else {
        return first.element;
    }

}

public Object getLast() {

    if (size == 0) {
        return null;
    } else {
        return last.element;
    }

}

public void addFirst(Object o) {

    Node newNode = new Node(o);
    newNode.next = first;
    first = newNode;
    size++;

    if (last == null) {
        last = first;
    }

}

public void addLast(Object o) {

    if (last == null) {
        last = first = new Node(o);
    } else {

        Node newNode = new Node(o);
        last.next = newNode;
        last = last.next;

    }

    size++;

}

public void add(int index, Object o) {

    if (index == 0) {

        addFirst(o);

    } else {

        if (index >= size) {
            addLast(o);

        } else {

            Node current = first;

            for (int i = 1; i < index; i++) {
                current = current.next;
            }

            Node temp1 = current.next;
            Node temp2 = new Node(o);
            current.next = temp2;
            temp2.next = temp1;
            size++;

        }

    }

}

public Object set(int index, Object o) {

    Node temp = new Node(o);

    if (index == 0) {
        first = temp;
    } else {

        Node current = first;

        for (int i = 1; i < index; i++) {
            current = current.next;
        }

        Node temp1 = current.next;
        current.next = temp;
        temp.next = temp1;

    }

    return o;

}

public void clear() {
    first = last = null;
}

public boolean contains(Object o) {

    Node current = first;

    while (current != null) {

        if (current.element.equals(o)) {
            return true;
        }

        current = current.next;

    }

    return false;

}

public Object get(int index) {

    Node current = first;

    for (int i = 0; i < index; i++) {
        current = current.next;
    }

    return current.element;

}

public int indexOf(Object o) {

    Node current = first;
    int index = -1;
    int i = 0;

    while (current != null) {

        if (current.element.equals(o)) {
            return i;
        }

        current = current.next;
        i++;
    }

    return index;

}

public int lastIndexOf(Object o) {

    Node current = first;
    int index = -1;
    int i = 0;

    while (current != null) {

        if (current.element.equals(o)) {
            index = i;
        }

        current = current.next;
        i++;

    }

    return index;

}

public boolean remove(Object o) {
    return remove(indexOf(o));
}

public boolean remove(int index) {

    if (index == 0) {

        first.next = first;
        return true;

    } else {

        if (index >= size) {
            return false;
        } else {

            Node current = first;

            for (int i = 1; i < index; i++) {
                current = current.next;
            }

            Node temp = current.next;
            current.next = temp.next;
            size--;
            return true;
        }

    }

}

public String toString() {

    StringBuffer result = new StringBuffer("[");

    Node current = first;

    for (int i = 0; i < size; i++) {
        result.append(current.element);
        current = current.next;

        if (current != null) {
            result.append(", ");
        } else {
            result.append("]");
        }
    }

    return result.toString();

}

}

// MyList接口类:

public interface MyList {

public void add(Object o);

public void add(int index, Object o);

public void clear();

public boolean contains(Object o);

public Object get(int index);

public int indexOf(Object o);

public boolean isEmpty();

public int lastIndexOf(Object o);

public boolean remove(Object o);

public boolean remove(int index);

public Object set(int index, Object o);

public int size();

}

// MyAbstractList抽象类:

public abstract class MyAbstractList implements List {

protected int size;

protected MyAbstractList() {
}

protected MyAbstractList(Object[] objects) {
    for (int i = 0; i < objects.length; i++) {
        this.add(objects[i]);
    }
}

public void add(Object o) {
    add(size, o);
}

public boolean isEmpty() {
    return size == 0;
}

public int size() {
    return size;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值