• 自己实现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+"";
            }
        }
    }
  • 相关阅读:
    BZOJ 2434 [NOI2011]阿狸的打字机
    BZOJ 3319 黑白树
    BZOJ 2500 幸福的道路
    BZOJ 3884 上帝与集合的正确用法
    BZOJ 3594[Scoi2014]方伯伯的玉米田
    BZOJ 3038上帝造题的七分钟2
    BZOJ 3887[Usaco2015 Jan]Grass Cownoisseur
    BZOJ 1875[SDOI2009]HH去散步
    看电影
    单峰
  • 原文地址:https://www.cnblogs.com/moris5013/p/12040389.html
Copyright © 2020-2023  润新知