基于Java关于链表的简单实现

之前写过集合的简单实现,今天来写一下链表的简单实现,实现了增删改查,写的不好

package com.levin.stack;

/**
 * 链表的简单实现
 * @ClassName LinkList
 * @Description TODO(这里用一句话描述这个类的作用)
 * @author Levin
 * @Date 2017年7月7日 下午5:56:30
 * @version 1.0.0
 * @param <E>
 */
public class LinkList<E> {
    private Node<E> firstNode;
    private Node<E> lastNode;
    private int size;

    /**
     * 链表新增方法
     * @Description 
     * @author Levin
     * @param o
     */
    public void add(E o) {
        if (firstNode == null) {
            Node<E> currentNode = new Node<E>(o);
            firstNode = currentNode;
            lastNode = currentNode;
        } else {
            Node<E> currentNode = new Node<E>(o);
            currentNode.previousNode = lastNode;
            if (lastNode.nextNode == null) {
                lastNode.nextNode = currentNode;
            }
        }
        size++;
    }
    /**
     * 按照下标获取
     * @Description (TODO这里用一句话描述这个方法的作用)
     * @author Levin
     * @param index
     * @return
     */
    public E get(int index) {
        if (!checkDataIndex(index)) {
            throw new ArrayIndexOutOfBoundsException(index);
        }
        Node<E> node = firstNode;
        for (int i = 0; i < index; i++) {
            node = node.nextNode;
        }
        return node.data;
    }

    /**
     * 检查下标是否可用
     * @Description (TODO这里用一句话描述这个方法的作用)
     * @author Levin
     * @param index
     * @return true为下标可用 false为下标不可用
     */
    public boolean checkDataIndex(int index) {
        return index >= 0 && index < size&&firstNode!=null;
    }

    /**
     * 链表删除方法
     * @Description (TODO这里用一句话描述这个方法的作用)
     * @author Levin
     * @param o
     */
    public void remove(E o) {
        Node<E> currNode = firstNode;
        if (firstNode == null) {
            return;
        }
        while (currNode != null) {
            if (firstNode.data.equals(o)) {
                firstNode = firstNode.nextNode;
                if (firstNode!=null) {
                    firstNode.previousNode=null;
                }
                currNode = firstNode;
                size--;
                continue;
            }
            if (currNode.data.equals(o)) {
                currNode.previousNode.nextNode = currNode.nextNode;
                currNode = currNode.nextNode;
                size--;
                continue;
            }
            currNode = currNode.nextNode;
        }
    }

    /**
     * 获取链表长度
     * @Description (TODO这里用一句话描述这个方法的作用)
     * @author Levin
     * @return
     */
    public int length() {
        return size;
    }

    @Override
    public String toString() {
        Node<E> currNode = firstNode;
        if (firstNode == null) {
            return null;
        }
        StringBuffer nodesBuffer = new StringBuffer();
        while (currNode != null) {
            nodesBuffer.append(currNode.data).append(",");
            currNode = currNode.nextNode;
        }
        return nodesBuffer.deleteCharAt(nodesBuffer.length() - 1).toString();
    }

    public static void main(String[] args) {
        LinkList<String> list = new LinkList<>();
        list.add("a");
        list.add("b");
        System.out.println(list.get(2));
        System.out.println(list);
    }

    private static class Node<E> {
        Node<E> nextNode;
        Node<E> previousNode;
        E data;

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

猜你喜欢

转载自blog.csdn.net/q2365921/article/details/74770013
今日推荐