自己实现数据结构---LinkedList

Posted inspred

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自己实现数据结构---LinkedList相关的知识,希望对你有一定的参考价值。

一.先上代码:

1.方式一:

public class LinkedList<E> {

    //节点,用来存放数据:数据+下一个元素的引用
    private class Node{
        private E e;
        private Node next;
        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }
        public Node(E e){
            this(e,null);
        }
        public Node(){
            this(null,null);
        }
        public String toString(){
            return e.toString();
        }
    }

    private Node head;
    private int size;

    /**
     * 构造方法
     */
    public LinkedList(){
        head = null;
        size = 0;
    }

    /**
     * 获取链表中元素的个数
     * @return
     */
    public int getSize(){
        return size;
    }

    /**
     * 判断链表是否为空
     * @return
     */
    public boolean isEmpty(){
        return size == 0;
    }

    /**
     * 链表头添加新元素
     */
    public void addFirst(E e){
        Node node = new Node();
        node.next = head;
        head = node;
        size ++;
    }

    /**
     * 链表中间添加元素
     * @param index
     * @param e
     */
    public void add(int index,E e){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        if (index == 0){
            addFirst(e);
        }else {
            Node prev = head;
            for (int i = 0 ; i < index-1; i++){
                prev = prev.next;
            }
            Node node = new Node(e);
            node.next = new Node(e);
            prev.next = node;
        }
        size++;
    }

    /**
     * 链表末尾添加元素
     * @param e
     */
    public void addList(E e){
        add(size,e);
    }


}

2.方式二:

package com.amazing.jdk.datastructure;

/**
 * Created by yaming on 18-6-21.
 */
public class LinkedListPlus<E> {

    //节点,用来存放数据:数据+下一个元素的引用
    private class Node{
        private E e;
        private Node next;
        public Node(E e,Node next){
            this.e = e;
            this.next = next;
        }
        public Node(E e){
            this(e,null);
        }
        public Node(){
            this(null,null);
        }
        public String toString(){
            return e.toString();
        }
    }

    private Node dummyHead;//虚拟头节点
    private int size;
    public LinkedListPlus(){
        dummyHead = new Node(null,null);
        size = 0;
    }

    public void add(int index,E e){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node node = new Node(e);
        node.next = prev.next;
        prev.next = node;
        size++;
    }

    public void addFirst(E e){
        add(0,e);
    }

    public void addLast(E e){
        add(size,e);
    }

    public E get(int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i <size ; i++) {
            cur = cur.next;
        }
        return cur.e;
    }

    public E getFirst(){
        return get(0);
    }

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

    public void set(int index,E e){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node cur = dummyHead.next;
        for (int i = 0; i <size ; i++) {
            cur = cur.next;
        }
        cur.e = e;
    }

    public boolean contains(E e){
        Node cur = dummyHead.next;
        while (cur != null){
            if (cur.e.equals(e)){
                return true;
            }
            cur = cur.next;
        }
        return false;

    }

    public E remove(int index){
        if (index < 0 || index > size){
            throw new IllegalArgumentException("Add Failed");
        }
        Node prev = dummyHead;
        for (int i = 0; i <index ; i++) {
            prev = prev.next;
        }
        Node retNode = prev.next;
        prev.next = retNode.next;
        retNode.next = null;
        size--;
        return retNode.e;
    }

    public E removeFirst(){
        return remove(0);
    }

    public E removeLast(){
        return remove(size-1);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        Node cur = dummyHead.next;
        while (cur != null){
            res.append(cur+"->");
            cur = cur.next;
        }
        res.append("null");
        return res.toString();
    }
}

 

以上是关于自己实现数据结构---LinkedList的主要内容,如果未能解决你的问题,请参考以下文章

JAVA自己实现LinkedList

自己实现Single LinkedList

自己实现LinkedList

LinkedList的实现

自己实现Linkedlist,实现其常用的增删查的方法

自己实现简单的linkedlist 没有迭代器