自己实现LinkedList

Posted moris5013

tags:

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

public class MyLinkedList<E> {

    private Node first;

    private int size;

    public int size(){
        return size;
    }

    @Override
    public String toString() {
       if(size == 0){
           return  "[]";
       }else{
           StringBuilder sb = new StringBuilder("[");
           Node current = first;
           while (current != null){
               sb.append(current.value).append(",");
               current = current.next;
           }
           sb.replace(sb.length() - 1, sb.length(), "]");
           return sb.toString();
       }
    }

    public static void main(String[] args) {
        MyLinkedList list = new MyLinkedList();
        list.addFirst("python").addFirst("java").addFirst("hello").addFirst("php");
        System.out.println(list);
        list.removeFirst();
        System.out.println(list);
        System.out.println(list.contains("python"));

    }
    public E removeFirst(){
        if(size == 0){
            return  null;
        }else {
            Node<E>  temp  =  first;
            first = temp.next;
            size--;
            return  temp.value;
        }
    }

    public   boolean contains(E e){
        Node current = first;
        while(current != null){
            if(current.value == e ){
                return true;
            }
            current = current.next;
        }
        return false;
    }

    public MyLinkedList addFirst(E e){
        Node newNode = new Node(e);
        newNode.next = first;
        first = newNode;
        size++;
        return this;
    }

    private  static  class  Node<E>{
        private E value;
        private Node next;

        Node(E value){
            this.value = value;
        }

        @Override
        public String toString() {
            return  value == null?"null":value+"";
        }
    }
}

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

自己实现Single LinkedList

自己实现LinkedList

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

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

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

12mmaction2 行为识别商用级别X3D复现 demo实现 检测自己的视频 Expanding Architecturesfor Efficient Video Recognition(代码片段