LinkedList集合里面的每一个节点都包含上一个和下一个元素,所以使用内部类会更方便,我这个LinkedList写的并不规范,修饰符和返回值也没有很好的定义,以后慢慢改进

LinkedList

package me.tateliao.List;

/**
 * 链表
 */
public class MyLinkedList {

    private Node first;

    private Node last;

    private int size;

    /**
     * 添加方法
     * @param object
     */
    public void add(Object object) {
        Node node = new Node();
        //判断是否为第一个节点
        if(first == null) {
            //如果是,那么这个节点(Node),里的object属性则为添加的值,而prev和next则为空,不赋值
            node.prev = null;
            node.object = object;
            node.next = null;
            //将这个节点赋值给MyLinkedList里的first属性
            first = node;
            last = node;
        } else {
            //直接往last节点后增加新的节点
            //所以Node里的first属性为MyLinkedList里的last属性,而Node里的Next属性为空
            node.prev = last;
            node.object = object;
            node.next = null;

            last.next = node;
            last = node;
        }
        size++;
    }

    public int length() {
        return size;
    }

    /**
     * 得到索引元素
     * @param index
     * @return
     */
    public Object get(int index) {
        return node(index).object;
    }

    /**
     * 删除节点
     * @param index
     */
    public void remove(int index) {
        Node temp = node(index);
        if (temp != null) {
            Node up = temp.prev;
            Node down = temp.next;
            up.next = down;
            down.prev = up;
        }
        size--;
    }

    /**
     * 插入一个新对象
     * @param index
     * @param object
     */
    public void add(int index, Object object) {
        Node newNode = new Node();
        newNode.object = object;
        Node temp = node(index);
        if (temp != null) {
            Node up = temp.prev;
            up.next = newNode;
            newNode.prev = up;

            newNode.next = temp;
            temp.prev = newNode;
        }
        size++;
    }

    /**
     * 设置当前节点
     * @param index
     * @param object
     */
    public void set(int index, Object object) {
        Node newNode = new Node();
        newNode.object = object;
        Node temp = node(index);
        if (temp != null) {
            Node up = temp.prev;
            up.next = newNode;

            Node down = temp.next;
            down.prev = newNode;

            newNode.prev = temp.prev;
            newNode.next = temp.next;
        }
    }

    /**
     * 检查索引
     * @param index
     */
    private void rangeCheck(int index) {
        if (index < 0 || index >= size) {
            try {
                throw new Exception();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 得到索引节点
     * @param index
     * @return
     */
    private Node node(int index) {
        rangeCheck(index);
        Node temp = null;
        if (first != null) {
            temp = first;
            for (int i = 0;i < index; i++) {
                temp = temp.next;
            }
        }
        return temp;
    }

}

/**
 * 用来表示一个节点
 */
class Node {

    Node prev;

    Object object;

    Node next;

}

测试类

package me.tateliao.test.List;

import me.tateliao.List.MyLinkedList;

import java.util.LinkedList;
import java.util.List;

/**
 * 测试LinkLis
 */
public class TestMyLinkedList {
    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.add("111");
        list.add("222");
        list.add("333");
        list.add("444");
        System.out.println(list.length());
        System.out.println(list.get(1));
        list.remove(1);
        list.add(1, "000");
        System.out.println(list.get(1));
        list.set(1, "666");
        System.out.println(list.get(1));
    }
}

返回结果

4
222
000
666